标签:
自定义imageview
package com.bwei.view; import com.bwei.main.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ImageView; /** * Created by Administrator on 2015/4/30. */ public class MLImageView extends ImageView { private Paint mPressPaint; private int mWidth; private int mHeight; private int mPressAlpha; private int mPressColor; private int mRadius; private int mShapeType; private int mBorderWidth; private int mBorderColor; public MLImageView(Context context) { super(context); init(context, null); } public MLImageView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public MLImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { //鍒濆鍖栭粯璁ゅ? mPressAlpha = 64; mPressColor = getResources().getColor(R.color.ml_gray); mRadius = 16; mShapeType = 1; mBorderWidth = 0; mBorderColor = getResources().getColor(R.color.ml_red); // 鑾峰彇鎺т欢鐨勫睘鎬у? if (attrs != null) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MLImageView); mPressColor = array.getColor(R.styleable.MLImageView_press_color, mPressColor); mPressAlpha = array.getInteger(R.styleable.MLImageView_press_alpha, mPressAlpha); mRadius = array.getDimensionPixelSize(R.styleable.MLImageView_radius, mRadius); mShapeType = array.getInteger(R.styleable.MLImageView_shape_type, mShapeType); mBorderWidth = array.getDimensionPixelOffset(R.styleable.MLImageView_border_width, mBorderWidth); mBorderColor = array.getColor(R.styleable.MLImageView_border_color, mBorderColor); array.recycle(); } // 鎸変笅鐨勭敾绗旇缃? mPressPaint = new Paint(); mPressPaint.setAntiAlias(true); mPressPaint.setStyle(Paint.Style.FILL); mPressPaint.setColor(mPressColor); mPressPaint.setAlpha(0); mPressPaint.setFlags(Paint.ANTI_ALIAS_FLAG); setClickable(true); setDrawingCacheEnabled(true); setWillNotDraw(false); } @Override protected void onDraw(Canvas canvas) { // super.onDraw(canvas); // 鑾峰彇褰撳墠鎺т欢鐨?drawable Drawable drawable = getDrawable(); if (drawable == null) { return; } // 杩欓噷 get 鍥炴潵鐨勫搴﹀拰楂樺害鏄綋鍓嶆帶浠剁浉瀵瑰簲鐨勫搴﹀拰楂樺害锛堝湪 xml 璁剧疆锛? if (getWidth() == 0 || getHeight() == 0) { return; } // 鑾峰彇 bitmap锛屽嵆浼犲叆 imageview 鐨?bitmap Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); drawDrawable(canvas, bitmap); drawPress(canvas); drawBorder(canvas); } private void drawDrawable(Canvas canvas, Bitmap bitmap) { // 鐢荤瑪 Paint paint = new Paint(); // 棰滆壊璁剧疆 paint.setColor(0xffffffff); // 鎶楅敮榻? paint.setAntiAlias(true); //Paint 鐨?Xfermode锛孭orterDuff.Mode.SRC_IN 鍙栦袱灞傚浘鍍忕殑浜ら泦閮ㄩ棬, 鍙樉绀轰笂灞傚浘鍍忋? PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); // 鏍囧織 int saveFlags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG; canvas.saveLayer(0, 0, mWidth, mHeight, null, saveFlags); if (mShapeType == 0) { // 鐢婚伄缃╋紝鐢诲嚭鏉ュ氨鏄竴涓拰绌洪棿澶у皬鐩稿尮閰嶇殑鍦? canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, paint); } else { // 褰揝hapeType = 1 鏃?鍥剧墖涓哄渾瑙掔煩褰? RectF rectf = new RectF(0, 0, getWidth(), getHeight()); canvas.drawRoundRect(rectf, mRadius, mRadius, paint); } paint.setXfermode(xfermode); // 绌洪棿鐨勫ぇ灏?/ bitmap 鐨勫ぇ灏?= bitmap 缂╂斁鐨勫?鏁? float scaleWidth = ((float) getWidth()) / bitmap.getWidth(); float scaleHeight = ((float) getHeight()) / bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); //bitmap 缂╂斁 bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); //draw 涓婂幓 canvas.drawBitmap(bitmap, 0, 0, paint); canvas.restore(); } private void drawPress(Canvas canvas) { if(mShapeType == 0){ canvas.drawCircle(mWidth/2, mHeight/2, mWidth/2, mPressPaint); }else if (mShapeType == 1) { RectF rectF = new RectF(0, 0, mWidth, mHeight); canvas.drawRoundRect(rectF, mRadius, mRadius, mPressPaint); } } private void drawBorder(Canvas canvas){ if(mBorderWidth > 0){ Paint paint = new Paint(); paint.setStrokeWidth(mBorderWidth); paint.setStyle(Paint.Style.STROKE); paint.setColor(mBorderColor); paint.setAntiAlias(true); if (mShapeType == 0) { canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, paint); } else { // 褰揝hapeType = 1 鏃?鍥剧墖涓哄渾瑙掔煩褰? RectF rectf = new RectF(0, 0, getWidth(), getHeight()); canvas.drawRoundRect(rectf, mRadius, mRadius, paint); } } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mPressPaint.setAlpha(mPressAlpha); invalidate(); break; case MotionEvent.ACTION_UP: mPressPaint.setAlpha(0); invalidate(); break; default: invalidate(); break; } return super.onTouchEvent(event); } }
values中创建attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!--自定义MLImageView的属性--> <declare-styleable name="MLImageView"> <attr name="press_alpha" format="integer" /> <attr name="press_color" format="color" /> <attr name="radius" format="dimension"/> <attr name="border_width" format="dimension" /> <attr name="border_color" format="color"/> <attr name="shape_type" format="enum"> <enum name="round" value="0"/> <enum name="rectangle" value="1" /> </attr> </declare-styleable> </resources>
values中创建colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="ml_white">#ffffffff</color> <color name="ml_gray">#ff9e9e9e</color> <color name="ml_blue">#ff239efe</color> <color name="ml_red">#ffff0000</color> </resources>
main中引用是加命名空间
xmlns:melove="http://schemas.android.com/apk/res-auto"
main中引用控件
方形 <com.bwie.yuanxingtupian.MLImageView android:layout_width="96dp" android:layout_height="96dp" android:layout_margin="8dp" android:src="@drawable/touxiang" melove:radius="8dp" melove:shape_type="rectangle" /> 圆形 <com.bwie.yuanxingtupian.MLImageView android:layout_width="96dp" android:layout_height="96dp" android:layout_margin="8dp" android:src="@drawable/touxiang" melove:shape_type="round" /> 方形加边框 <com.bwie.yuanxingtupian.MLImageView android:layout_width="96dp" android:layout_height="96dp" android:layout_margin="8dp" android:src="@drawable/touxiang" melove:border_color="@color/ml_white" melove:border_width="4dp" melove:press_alpha="50" melove:press_color="#00ff00" melove:radius="8dp" melove:shape_type="rectangle" /> 圆形加边框 <com.bwie.yuanxingtupian.MLImageView android:layout_width="90dp" android:layout_height="90dp" android:layout_margin="8dp" android:src="@drawable/touxiang" melove:border_color="@color/ml_white" melove:border_width="4dp" melove:shape_type="round" />
标签:
原文地址:http://www.cnblogs.com/gaoliangjie/p/5543741.html