本例子是针对将图片两个直角转换为圆角的例子:
1.自定义3个属性 宽高,圆角值:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyPhotoView"> <attr name="width" format="dimension"></attr> <attr name="height" format="dimension"></attr> <attr name="radus" format="dimension"></attr> </declare-styleable> </resources>
2.在引用的布局中使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bc="http://schemas.android.com/apk/res/org.lean" android:layout_width="match_parent" android:layout_height="match_parent" > <org.lean.MyPhotoView android:id="@+id/photo_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="60dp" bc:width="160dp" bc:height="240dp" bc:radus="10dp" /> </RelativeLayout>
3.在Java代码中设置两个图层。这里有2个图片 先绘制的叫目标图层,然后设置混合渲染绘制 ,后绘制的图层成为源图层。
SRC_IN 模式在两者相交的地方绘制源图层(同时隐藏目标图层),并且绘制的效果会受到目标图像对应地方透明度的影响;
package org.lean; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.widget.ImageView; /** * 重绘的ImageView * * @author Lean */ public class MyPhotoView extends ImageView{ private Bitmap mImageBtp; private Rect mImageSrcRect ,mImageDesRect; private Rect mBtmRect; private float mTotalWidth; private float mTotalHeight; private float mRadus; public MyPhotoView(Context context,int with,int height,float radus) { super(context); mTotalWidth=with; mTotalHeight=height; mRadus=radus; } public MyPhotoView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyPhotoView); mTotalWidth = array.getDimensionPixelSize(R.styleable.MyPhotoView_width, 0); mTotalHeight = array.getDimensionPixelSize(R.styleable.MyPhotoView_height, 0); mRadus = array.getDimensionPixelSize(R.styleable.MyPhotoView_radus, 0); array.recycle(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mImageDesRect=new Rect(0, 0,(int)mTotalWidth, (int)mTotalHeight); mBtmRect=new Rect(0, 0,(int)mTotalWidth,(int)(mTotalHeight+mRadus)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG); PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); canvas.drawColor(Color.WHITE); int saveCount=canvas.saveLayer(0,0, mTotalWidth,mTotalHeight,paint,Canvas.ALL_SAVE_FLAG); if (mImageBtp!=null) { paint.setColor(Color.WHITE); canvas.drawRoundRect(new RectF(mBtmRect), mRadus, mRadus, paint); paint.setXfermode(xfermode); canvas.drawBitmap(mImageBtp,mImageSrcRect,mImageDesRect,paint); paint.setXfermode(null); } canvas.restoreToCount(saveCount); } @Override public void setImageBitmap(Bitmap bm) { super.setImageBitmap(bm); mImageBtp=bm; reCalculateRect(); } private void reCalculateRect() { mImageSrcRect=new Rect(0, 0,mImageBtp.getWidth(),mImageBtp.getHeight()); invalidate(); } @Override public void setImageResource(int resId) { super.setImageResource(resId); mImageBtp=((BitmapDrawable)getResources().getDrawable(resId)).getBitmap(); reCalculateRect(); } }
时间: 2024-10-10 16:40:14