Android實用檢視動畫及工具系列之九:漂亮的圖片選擇器,高效能防崩潰圖片選擇工具
阿新 • • 發佈:2019-02-07
public class MainActivity extends AppCompatActivity { private Button btn1, btn2; private TextView tv1; private ArrayList<String> path = new ArrayList<>(); public static final int REQUEST_CODE = 123; private ImageConfig imageConfig; private LinearLayout llContainer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); tv1 = (TextView) findViewById(R.id.tv1); llContainer = (LinearLayout) findViewById(R.id.llContainer); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageConfig = new ImageConfig.Builder( new GlideLoader()) .steepToolBarColor(getResources().getColor(R.color.titleBlue)) .titleBgColor(getResources().getColor(R.color.titleBlue)) .titleSubmitTextColor(getResources().getColor(R.color.white)) .titleTextColor(getResources().getColor(R.color.white)) // 開啟單選 (預設為多選) .singleSelect() // 裁剪 (只有單選可裁剪) //.crop() // 開啟拍照功能 (預設關閉) .showCamera() // 設定顯示容器 .setContainer(llContainer) .requestCode(REQUEST_CODE) .build(); ImageSelector.open(MainActivity.this, imageConfig); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageConfig = new ImageConfig.Builder( new GlideLoader()) .steepToolBarColor(getResources().getColor(R.color.titleBlue)) .titleBgColor(getResources().getColor(R.color.titleBlue)) .titleSubmitTextColor(getResources().getColor(R.color.white)) .titleTextColor(getResources().getColor(R.color.white)) // 開啟多選 (預設為多選) .mutiSelect() // 多選時的最大數量 (預設 9 張) .mutiSelectMaxSize(9) // 設定圖片顯示容器,引數:(1、顯示容器,2、每行顯示數量(建議不要超過8個),是否可刪除) .setContainer(llContainer, 4, true) // 已選擇的圖片路徑 .pathList(path) // 拍照後存放的圖片路徑(預設 /temp/picture) .filePath("/temp") // 開啟拍照功能 (預設關閉) .showCamera() .requestCode(REQUEST_CODE) .build(); ImageSelector.open(MainActivity.this, imageConfig); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) { List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT); tv1.setText(""); for (String path : pathList) { tv1.append(path); tv1.append("\n"); } path.clear(); path.addAll(pathList); } } }