Android最简单的LoadingDialog

节选自:Android最简单的LoadingDialog


Activity的基类

public class BaseAcitivity extends Activity {

    private AlertDialog alertDialog;

    public void showLoadingDialog() {
        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
        alertDialog.setCancelable(false);
        alertDialog.show();
        alertDialog.setContentView(R.layout.loading_alert);
        alertDialog.setCanceledOnTouchOutside(false);
    }

    public void dismissLoadingDialog() {
        if (null != alertDialog && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }
}

XML:loading_alert

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <ProgressBar
        style="@style/AppTheme.NoActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal" />

</RelativeLayout>

在styles.xml中添加

<style name="AppTheme.NoActionBar">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

安卓-连续点击两次返回键退出程序-二级界面的退出程序

节选自:安卓-连续点击两次返回键退出程序-二级界面的退出程序


法一:实现方式,通过记录按键时间计算时间差实现:

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}

Android获取相机最佳预览大小和获取屏幕宽高

/**
 * 获取最佳预览大小
 *
 * @param parameters       相机参数
 * @param screenResolution 屏幕宽高
 * @return
 */
private Point getBestCameraResolution(Camera.Parameters parameters, Point screenResolution) {
    float tmp;
    float mindiff = 100f;
    float x_d_y = (float) screenResolution.x / (float) screenResolution.y;
    Size best = null;
    List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
    for (Size s : supportedPreviewSizes) {
        tmp = Math.abs(((float) s.height / (float) s.width) - x_d_y);
        if (tmp < mindiff) {
            mindiff = tmp;
            best = s;
        }
    }
    return new Point(best.width, best.height);
}

/**
 * 获取屏幕宽度和高度,单位为px
 *
 * @param context
 * @return
 */
public static Point getScreenMetrics(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    return new Point(dm.widthPixels, dm.heightPixels);
}

Android使用POI读取Excel(XLS和XLSX)

1.添加依赖

// Excel
implementation 'org.apache.poi:poi:3.17'
implementation 'org.apache.poi:poi-ooxml:3.17'
implementation 'org.apache.xmlbeans:xmlbeans:3.1.0'
implementation 'stax:stax:1.2.0'

2.在程序中使用

private void readExcel(String fileName) {
    try {
        InputStream inputStream = new FileInputStream(fileName);
        Workbook workbook;
        if (fileName.endsWith(".xls")) {
            workbook = new HSSFWorkbook(inputStream);
        } else if (fileName.endsWith(".xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else {
            return;
        }
        Sheet sheet = workbook.getSheetAt(0);
        int rowsCount = sheet.getPhysicalNumberOfRows();
        FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
        for (int r = 0; r < rowsCount; r++) {
            Row row = sheet.getRow(r);
            CellValue v0 = formulaEvaluator.evaluate(row.getCell(0));
            CellValue v1 = formulaEvaluator.evaluate(row.getCell(1));
            Log.i("Excel", "readExcel: " + v0.getStringValue() + "," + v1.getStringValue());
        }
        workbook.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Android 依赖冲突(Gradle查看依赖树)

节选自:Android依赖冲突解决方法


依赖分析

查看依赖关系需要用到的命令为:

gradlew :[module_name]:dependencies

如需分析工程中app这个module的依赖关系行命令则为:

gradlew :app:dependencies

如果你想把依赖关系输出到文件中,则可以使用以下命令:

gradlew :[module_name]:dependencies > [output_file]

例如将app module的依赖关系输出到dependence.txt文件中:

gradlew :app:dependencies > dependence.txt

简化版Gradle查看依赖树命令(Gradle查看依赖树解惑

gradlew :app:dependencies --configuration releaseCompileClasspath > dependence.txt

Android图片加载框架最全解析

Android图片加载框架最全解析(一),Glide的基本用法

Android图片加载框架最全解析(二),从源码的角度理解Glide的执行流程

Android图片加载框架最全解析(三),深入探究Glide的缓存机制

Android图片加载框架最全解析(四),玩转Glide的回调与监听

Android图片加载框架最全解析(五),Glide强大的图片变换功能

Android图片加载框架最全解析(六),探究Glide的自定义模块功能

Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能

Android图片加载框架最全解析(八),带你全面了解Glide 4的用法