日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义图片形状

發(fā)布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义图片形状 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

自定義圖片形狀(通過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é)

以上是生活随笔為你收集整理的自定义图片形状的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。