自定义图片形状
自定義圖片形狀(通過ShapeDrawable):
import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.ArcShape; import android.graphics.drawable.shapes.OvalShape; import android.graphics.drawable.shapes.PathShape; import android.graphics.drawable.shapes.RectShape; import android.graphics.drawable.shapes.RoundRectShape; import android.util.AttributeSet; import android.view.View;public class ShapeView extends View{private Paint paint;private BitmapShader bitmapShader;private Bitmap bitmap;private ShapeDrawable shapeDrawable;private int centerx,centery,radius;/** 默認類型為圓形*/private int type=CIRCLE_BITMAP;private Path path;/** 外矩形 圓角半徑 */private float[] outerR;/** 內(nèi)部矩形與外部矩形的距離 */private RectF inset;/** 內(nèi)矩形 圓角半徑 */private float[] innerRadii = {20, 20, 20, 20, 20, 20, 20, 20};/** 圓形圖片*/public static final int CIRCLE_BITMAP=11;/** 矩形圖片*/public static final int RECT_BITMAP=12;/** 橢圓圖片*/public static final int OVAL_BITMAP=13;/** 圓角圖片*/public static final int ROUNDRECT_BITMAP=14;/** 弧形圖片*/public static final int ARC_BITMAP=15;/** path路徑圖片*/public static final int PATH_BITMAP=16;public ShapeView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public ShapeView(Context context, AttributeSet attrs) {super(context, attrs);}public ShapeView(Context context) {super(context);}/*** 設(shè)置圖片類型* @param type*/public void SetType(int type){this.type=type;}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {centerx=w/2;centery=h/2;radius=Math.min(centerx,centery);paint=new Paint();bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.shape)).getBitmap();// 構(gòu)造渲染器BitmapShader bitmapShader=new android.graphics.BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {switch (type) {case CIRCLE_BITMAP: // paint.setShader(bitmapShader); // canvas.drawCircle(centerx,centery, radius, paint);shapeDrawable=new ShapeDrawable(new OvalShape());//添加橢圓模型shapeDrawable.setBounds(0, 0, radius*2, radius*2);//設(shè)置顯示區(qū)域 為圓形break;case RECT_BITMAP:shapeDrawable=new ShapeDrawable(new RectShape());//添加矩形模型shapeDrawable.setBounds(0, 0, radius*2, radius*2);//設(shè)置顯示區(qū)域 為圓形break;case OVAL_BITMAP:shapeDrawable=new ShapeDrawable(new OvalShape());//添加橢圓模型shapeDrawable.setBounds(0, 0, centerx*2, centery*2);//設(shè)置顯示區(qū)域 為矩形break;case ROUNDRECT_BITMAP://外矩形 左上、右上、右下、左下 圓角半徑 outerR = new float[] { 50, 50, 50, 50, 50, 50, 50, 50};//內(nèi)矩形距外矩形,左上角x,y距離, 右下角x,y距離 , 內(nèi)部矩形與外部矩形的距離 inset = new RectF(150,150, 200,200); shapeDrawable=new ShapeDrawable(new RoundRectShape(outerR,null, null));//添加圓角模型 // shapeDrawable=new ShapeDrawable(new RoundRectShape(outerR,inset, innerRadii));//添加圓角模型shapeDrawable.setBounds(0, 0, centerx*2, centery*2);//設(shè)置顯示區(qū)域 為矩形break;case ARC_BITMAP:shapeDrawable=new ShapeDrawable(new ArcShape(135, 270));//添加弧形模型 (135:開始角度,270:掃描角度)shapeDrawable.setBounds(0, 0, radius*2, radius*2);//設(shè)置顯示區(qū)域 為圓形break;case PATH_BITMAP://繪制一個頂點為下列四個點的棱形 path = new Path(); path.moveTo(50, 0); path.lineTo(0, 50); path.lineTo(50, 100); path.lineTo(100, 50); shapeDrawable=new ShapeDrawable(new PathShape(path, 100, 200));//添加幾何路徑 // 100, 200 最終呈現(xiàn)出的坐標值(實際內(nèi)部是縮放的canvas)shapeDrawable.setBounds(0, 0, radius*2, radius*2);//設(shè)置顯示區(qū)域 為圓形break;default:break;}//得到畫筆并設(shè)置渲染器 shapeDrawable.getPaint().setShader(bitmapShader);//繪制shapeDrawable shapeDrawable.draw(canvas);} }關(guān)于shape:
有位盆友寫的挺詳細,有興趣的可以看看:http://blog.csdn.net/jjwwmlp456/article/details/46928859
總結(jié)
- 上一篇: simulink仿真结果出现振荡
- 下一篇: VA 破解版