android_takephoto的简单使用
GitHub https://github.com/crazycodeboy/TakePhoto
使用方法
compile 'com.jph.takephoto:takephoto_library:4.0.3'
效果图
注意效果图和代码不符 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_web" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.iamchan.takephoto.WebActivity"> <Button android:text="按钮" android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" />//按钮 <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="150dp" />//图片 </LinearLayout>
activity
public class WebActivity extends TakePhotoActivity {//采用继承方式获取图片 需要重写三个方法
private Button btn; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); btn=findViewById(R.id.btn); img=findViewById(R.id.img); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onClickk(getTakePhoto());//点击按钮获取图片 } }); } @Override public void takeSuccess(TResult result) {//第一种方法 成功 super.takeSuccess(result); Glide.with(this).load(new File(result.getImages().get(0).getCompressPath())).into(img);//成功选择图片之后用glide加载到imgView中 } @Override public void takeFail(TResult result, String msg) {//第二种 失败 super.takeFail(result, msg); } @Override public void takeCancel() {//第三种退出 主要看第一种成功 super.takeCancel(); } public void onClickk(TakePhoto takePhoto) { configCompress(takePhoto); takePhoto.onPickFromGallery(); } private void configCompress(TakePhoto takePhoto) {//压缩配置 int maxSize = Integer.parseInt("409600");//最大 压缩 int width = Integer.parseInt("800");//宽 int height = Integer.parseInt("800");//高 CompressConfig config; config = new CompressConfig.Builder().setMaxSize(maxSize) .setMaxPixel(width >= height ? width : height) .enableReserveRaw(false)//拍照压缩后是否显示原图 .create(); takePhoto.onEnableCompress(config, false);//是否显示进度条 }
}
这是加载一张图片的方法下一篇会讲加多张的方法