头像画小圆点

来源:互联网 发布:新mac pro 编辑:程序博客网 时间:2024/05/16 08:05

shape画小圆点


<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval"    android:useLevel="false">    <solid android:color="@color/while_01" />    <padding        android:bottom="1dp"        android:left="2dp"        android:right="2dp"        android:top="1dp" />    <solid android:color="@color/while_01" />    <stroke        android:width="1dp"        android:color="@android:color/white" />    <!--这里宽高要一样 -->    <size        android:width="15dp"        android:height="15dp" /></shape>


拍照从相册获取图片

<TextView    android:layout_width="match_parent"    android:layout_height="50dp"    android:background="#800080"    android:gravity="center"    android:text="个人信息"    android:textColor="#000000" /><RelativeLayout    android:id="@+id/rel_01"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:id="@+id/touxiang_01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="头像"        android:textColor="#000000" />    <ImageView         android:background="@mipmap/ic_launcher"        android:id="@+id/image_01"        android:layout_width="50dp"        android:layout_height="50dp"        android:layout_alignParentRight="true"        /></RelativeLayout><ImageView    android:layout_width="match_parent"    android:layout_height="5dp"    android:background="#C2C2C2"></ImageView><RelativeLayout    android:id="@+id/rel_02"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="用户名"        android:textColor="#000000" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_margin="10dp"        android:text="555" /></RelativeLayout><ImageView    android:layout_width="match_parent"    android:layout_height="5dp"    android:background="#C2C2C2"></ImageView><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="昵称"        android:textColor="#000000" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_margin="10dp"        android:text="555" /></RelativeLayout><ImageView    android:layout_width="match_parent"    android:layout_height="5dp"    android:background="#C2C2C2"></ImageView><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="性别"        android:textColor="#000000" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_margin="10dp"        android:text="555" /></RelativeLayout><ImageView    android:layout_width="match_parent"    android:layout_height="5dp"    android:background="#C2C2C2"></ImageView><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="出生日期"        android:textColor="#000000" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_margin="10dp"        android:text="555" /></RelativeLayout><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="165dp"    android:orientation="vertical">    <Button        android:id="@+id/btn_paizhao"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="拍照" />    <Button        android:id="@+id/btn_huoqu"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="从相册获取" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="取消" /></LinearLayout>




public class MainActivity extends AppCompatActivity implements View.OnClickListener {    protected static final int CHOOSE_PICTURE = 0;    protected static final int TAKE_PICTURE = 1;    private static final int CROP_SMALL_PICTURE = 2;    protected static Uri tempUri;    private Button mPaizhaoBtn;    private Button mHuoquBtn;    private ImageView image_01;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        //控件        image_01 = (ImageView) findViewById(R.id.image_01);        mPaizhaoBtn = (Button) findViewById(R.id.btn_paizhao);        mPaizhaoBtn.setOnClickListener(this);        mHuoquBtn = (Button) findViewById(R.id.btn_huoqu);        mHuoquBtn.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            //拍照            case R.id.btn_paizhao:                Intent openCameraIntent = new Intent(                        MediaStore.ACTION_IMAGE_CAPTURE);                tempUri = Uri.fromFile(new File(Environment                        .getExternalStorageDirectory(), "image.jpg"));                // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);                startActivityForResult(openCameraIntent, TAKE_PICTURE);                break;            //获取图片            case R.id.btn_huoqu:                Intent openAlbumIntent = new Intent(                        Intent.ACTION_GET_CONTENT);                openAlbumIntent.setType("image/*");                startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);                break;            default:                break;        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (resultCode == RESULT_OK) { // 如果返回码是可以用的            switch (requestCode) {                case TAKE_PICTURE:                    startPhotoZoom(tempUri); // 开始对图片进行裁剪处理                    break;                case CHOOSE_PICTURE:                    startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理                    break;                case CROP_SMALL_PICTURE:                    if (data != null) {                        setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上                    }                    break;            }        }    }    /**     * 裁剪图片方法实现     *     * @param uri     */    protected void startPhotoZoom(Uri uri) {        if (uri == null) {            Log.i("tag", "The uri is not exist.");        }        tempUri = uri;        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, "image/*");        // 设置裁剪        intent.putExtra("crop", "true");        // aspectX aspectY 是宽高的比例        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        // outputX outputY 是裁剪图片宽高        intent.putExtra("outputX", 150);        intent.putExtra("outputY", 150);        intent.putExtra("return-data", true);        startActivityForResult(intent, CROP_SMALL_PICTURE);    }    /**     * 保存裁剪之后的图片数据     *     * @param     * @param     */    protected void setImageToView(Intent data) {        Bundle extras = data.getExtras();        if (extras != null) {            Bitmap photo = extras.getParcelable("data");            photo = util.toRoundBitmap(photo, tempUri); // 这个时候的图片已经被处理成圆形的了            image_01.setImageBitmap(photo);        }    }}



转换圆形图片

public class util {    public static Bitmap toRoundBitmap(Bitmap bitmap, Uri tempUri) {        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;    }}