Android头像选择器

    之前的项目中有做过头像上传的功能,这次又有用到,就整理一下。

public class MainActivity extends Activity {
    private ImageView ivHead;
    private Bitmap head;
    private static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZKT/";

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

    private void initView() {
        ivHead = (ImageView) findViewById(R.id.imageView1);
        Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");
        if (bt != null) {
            Drawable drawable = new BitmapDrawable(bt);
            ivHead.setImageDrawable(drawable);
        } else {
            /**
             *从服务器获取
             *
             */
        }
    }

    private void initDialog() {
        final View view = getLayoutInflater().inflate(R.layout.selectdialog,
                null);
        final Dialog dialog = new Dialog(MainActivity.this);
//        取消dialog的title栏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        ivHead.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.setContentView(view, new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                Window window = dialog.getWindow();
                window.setWindowAnimations(R.style.anim);
                WindowManager.LayoutParams wl = window.getAttributes();
                wl.x = 0;
                wl.y = getWindowManager().getDefaultDisplay().getHeight();
                wl.width = LayoutParams.MATCH_PARENT;
                wl.height = LayoutParams.WRAP_CONTENT;
                dialog.onWindowAttributesChanged(wl);
                dialog.setCanceledOnTouchOutside(true);
                view.findViewById(R.id.btn_photochoose_cancle).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                view.findViewById(R.id.btn_photochoose_photobox).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                        intent1.setDataAndType(
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                        startActivityForResult(intent1, 1);
                        dialog.dismiss();
                    }
                });

                view.findViewById(R.id.btn_photochoose_take).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri
                                .fromFile(new File(Environment
                                        .getExternalStorageDirectory(), "head.jpg")));
                        startActivityForResult(intent2, 2);
                        dialog.dismiss();
                    }
                });
                dialog.show();

            }
        });


    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    cropPhoto(data.getData());
                }
                break;
            case 2:
                if (resultCode == RESULT_OK) {
                    File temp = new File(Environment.getExternalStorageDirectory()
                            + "/head.jpg");
                    cropPhoto(Uri.fromFile(temp));
                }
                break;
            case 3:
                if (data != null) {
                    Bundle extras = data.getExtras();
                    head = extras.getParcelable("data");
                    if (head != null) {
                        /**
                         * 上传到服务器
                         */
                        saveImage(head);
//                        设置给imageview
                        ivHead.setImageBitmap(head);
                    }
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //裁剪图片
    public void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 3);
    }

    //保存图片到本地
    private void saveImage(Bitmap mBitmap) {
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
            return;
        }
        FileOutputStream b = null;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = path + "head.jpg";
        try {
            b = new FileOutputStream(fileName);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                b.flush();
                b.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这是弹出,弹回dialog的动画代码:

  

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate  
    android:duration="300"
    android:fromXDelta="0"  
    android:fromYDelta="1000"  
    android:toXDelta="0"  
    android:toYDelta="0" /> 

</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate  
       android:duration="300"
       android:fromXDelta="0"  
       android:fromYDelta="0"  
       android:toXDelta="0"  
       android:toYDelta="1000" />

</set>

这是头像选择的dialog的布局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#00000000"  
    android:gravity="bottom"  
    android:orientation="vertical"  
   >
    <Button
        android:id="@+id/btn_photochoose_photobox"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:background="#ffffff"  
        android:paddingBottom="10dip"  
        android:paddingTop="10dip"  
        android:text="图库"  
        android:textSize="16sp" />  
  
    <TextView  
        android:layout_width="match_parent"  
        android:layout_height="0.5dip"  
        android:background="#DAD9DB" />  
  
    <Button  
        android:id="@+id/btn_photochoose_take"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:background="#ffffff"  
        android:paddingBottom="10dip"  
        android:paddingTop="10dip"  
        android:text="拍照"  
        android:textSize="16sp" />  
    <TextView  
        android:layout_width="match_parent"  
        android:layout_height="0.5dip"  
        android:background="#DAD9DB" />  
    <Button  
        android:id="@+id/btn_photochoose_cancle"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:background="#ffffff"
        android:paddingBottom="10dip"  
        android:paddingTop="10dip"  
        android:text="取消"  
        android:textSize="16sp" />  
  
</LinearLayout> 

Mainactivity的布局很简单,只有一个ImageView。希望能对初学者有帮助

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android图片选择是一种常用的工具,它可以帮助开发者在Android应用中实现图片选择的功能。通过图片选择,用户可以从相册或者其他存储位置中选择图片,并将其用于应用中的各种场景,比如头像上传、图片展示等。 Android平台上有很多开源的图片选择库可供使用,其中比较常用的有以下几个: 1. Glide:Glide是一个强大的图片加载和缓存库,它不仅可以加载网络图片,还可以加载本地图片和资源文件中的图片。Glide提供了简单易用的API,可以方便地实现图片选择和展示功能。 2. Picasso:Picasso是另一个流行的图片加载库,它也提供了简单易用的API,并且具有自动内存和磁盘缓存的功能。Picasso支持从网络、本地和资源文件中加载图片,并且可以自动处理图片的缩放和裁剪。 3. UCrop:UCrop是一个强大的图片裁剪库,它可以帮助开发者实现图片裁剪的功能。UCrop支持手势缩放、旋转和裁剪操作,并且提供了丰富的配置选项,可以满足不同场景下的需求。 4. Matisse:Matisse是一个功能丰富的图片选择库,它提供了多种样式的图片选择界面,并且支持多选、预览和裁剪等功能。Matisse还支持自定义主题和样式,可以根据应用的需求进行个性化定制。 以上是一些常用的Android图片选择库,它们都具有不同的特点和功能,开发者可以根据自己的需求选择合适的库来实现图片选择功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值