Android将图片打成圆形

昨天夜里十一点多刷空间看见我学长,在公司做了一个APP,挺好看的。我主要是看见了在他的APP中有个头像是一个圆形的,然后就嘟囔着说学学吧,昨天晚上都快12点了算了还是今天做吧。今儿一醒来八点多了,刷完牙就喝了杯浓茶,干!!!


想起在平常的APP都是可以更换头像的,更换头像一般都是从两部分进行选取图片,一个就是从相册提取另一个就是拍摄照片。然后就查资料。这个主要是参考两位的资料,谢谢。http://blog.csdn.net/swust_chenpeng/article/details/10898451     http://www.eoeandroid.com/forum.php?mod=viewthread&tid=238609&fromuid=777541 。

我的作用就是将这两篇文章综合了一下,实现了点击Button进行选取图片然后进行将图片打成圆形。


我的代码主要是分为两个类,一个就是MainActivity 这个就是对相册的查找和相机的打开,当打开相册选择照片以后就会把数据界面的结果中,然后再进行操作,另一个就是对圆变得处理的方法,我看了半天不知到它是怎么实现的,代码不短,我干脆把它写成了一个单独的类。这个方法传递进去的参数就是一个Bitmap 对象,返回的结果同样也是Bitmap的对象,还是比较简单的,既然我封装了,以后就这个用好了。


在相册提取照片的时候会出现OOM的问题,需要优化。

1、看看这个主要的圆形处理方法吧

package com.example.hejingzhou.getbitmapdemo;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * Created by Hejingzhou on 2016/4/10.
 *
 * 圆形化图形工具类
 */
public class RoundTools {
    public Bitmap toRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float roundPx;
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
        if (width <= height) {
            roundPx = width / 2;
            top = 0;
            bottom = width;
            left = 0;
            right = width;
            height = width;
            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else {
            roundPx = height / 2;
            float clip = (width - height) / 2;
            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;
            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }

        Bitmap output = Bitmap.createBitmap(width,
                height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
        final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
        final RectF rectF = new RectF(dst);

        paint.setAntiAlias(true);

        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return output;
    }
}
这个类就不解释了,因为我也不会。



2、MainActivity 这个主要就是打开相册,或相机进行选择图片。

这里面就是一个大问题,如果你是按照常规的方法写的话,如果你是按照常规的方法写的话,

 Intent intentCamer = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intentCamer, SELECT_CAMER);
这样发送后,在有些手机里并不能收到data。一看网上有好多这样的问题。弄了将近俩小时没什么好的方法,网上有很多种方法,试了三四种都不行,然后就Google了一下,终于在Stack Overflow找到了一个好的方法。
写上去后就Ok了。

就是在打开相机照完照片后,在protected void onActivityResult(int requestCode, int resultCode, Intent data) {} 重写的这个类是接收不到数据的,也就是data.getData();这个总是返回空,


package com.example.hejingzhou.getbitmapdemo;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = getClass().getSimpleName();
    private Button btnGetAlbum, btnGetCamera;
    private ImageView imgAlbum, imgCamer,imgRoundAlbum,imgRoundCamer;
    private int SELECT_PICTURE = 0x00;
    private int SELECT_CAMER = 0x01;
    private Bitmap bitmap;
    private RoundTools roundTools;

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

    /**
     * 初始化控件
     */
    private void findViewById() {
        btnGetAlbum = (Button) findViewById(R.id.buttonGetAlbum);
        btnGetAlbum.setOnClickListener(this);
        btnGetCamera = (Button) findViewById(R.id.buttonGetCamera);
        btnGetCamera.setOnClickListener(this);
        imgAlbum = (ImageView) findViewById(R.id.imageViewAlbum);
        imgCamer = (ImageView) findViewById(R.id.imageViewCamera);
        imgRoundAlbum = (ImageView)findViewById(R.id.imageViewRoundAlbum);
        imgRoundCamer = (ImageView)findViewById(R.id.imageViewRoundCamera);
    }

    /**
     * Button监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonGetAlbum://通过相册选择图片
                Intent intentAlbum = new Intent(Intent.ACTION_GET_CONTENT);
                intentAlbum.addCategory(intentAlbum.CATEGORY_OPENABLE);
                intentAlbum.setType("image/*");
                startActivityForResult(intentAlbum.createChooser(intentAlbum, "选择图片"), SELECT_PICTURE);
                break;
            case R.id.buttonGetCamera://通过相机选取照片
                Intent intentCamer = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intentCamer, SELECT_CAMER);
                break;
        }
    }

    /**
     * 返回结果处理
     *
     * @param requestCode 请求代码
     * @param resultCode  结果代码
     * @param data        返回数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SELECT_PICTURE) {
            Log.i(TAG, "相册");
            handle(resultCode, data);
            imgAlbum.setImageBitmap(bitmap);
            roundTools = new RoundTools();
            Bitmap roundAlbum = roundTools.toRoundBitmap(bitmap);
            imgRoundAlbum.setImageBitmap(roundAlbum);
        } else if (requestCode == SELECT_CAMER) {
            Log.i(TAG, "相机");
            /**
             * 一般手机data会完美的有数据,但是我的魅族就不行,网上查了查,还有一些手机都有这样的
             * 问题比如三星和小米 。那么我们就用  这种方式获取bitmap
             *
             * if (data.getData() == null) {
             *       bitmap = (Bitmap) data.getExtras().get("data");
             * } else try {
             *      bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
             *   } catch (IOException e) {
             *     e.printStackTrace();
             *     }
             */
            if (data.getData() == null) {
                bitmap = (Bitmap) data.getExtras().get("data");
                Log.i(TAG, "BitData    " + bitmap);
                imgCamer.setImageBitmap(bitmap);
                roundTools = new RoundTools();
                Bitmap roundCamer = roundTools.toRoundBitmap(bitmap);
                imgRoundCamer.setImageBitmap(roundCamer);
            } else try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
                if (bitmap != bitmap) {//主要是防止handle处理出错,就会将先前获取相册的照片show出来
                    imgCamer.setImageBitmap(bitmap);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 数据处理 共同点提取
     *
     * @param resultCode
     * @param data
     */
    private void handle(int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {//结果代码是Ok的
            Uri uri = data.getData();
            if (uri != null && data.getData() != null) {
                Log.i(TAG, "uri 和 data.getData()不为空");
                ContentResolver contentResolver = this.getContentResolver();
                if (bitmap != null) {
                    bitmap.recycle();
                }
                try {
                    bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));//出错
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i(TAG, "uri为空或者data为空   " + "数据:" + data.getData() + "  uri: " + uri);
            }
        }
    }
}

布局文件就不写了,开代码能才出来。
这是我学长写的。                                            我写的这个代码:
          




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值