自定义View
- 画布
- 1.Bitmap
- 1.1创建Bitmap方法一:BitmapFactory
- 1.2创建Bitmap方法二:Bitmap静态方法(createBitmap)
- 成果展示
- 1.自定义View_菱形图片
画布
创建画布的方式有:系统自带的onDraw(),diapatchDraw()函数,还可以通过:
- 通过Bitmap创建
- 通过SurfaceView的SurfaceHolder。lockCanvas()函数获取
https://www.jianshu.com/p/1a2d0acecffbhttps://www.jianshu.com/p/612917765568
1.Bitmap
用Bitmap自建画布:
Bitmap bitmap = Bitmap.createBitmap(200,100,Bitmap.Config.ARGB_8888);
Canvas my_canvas = new canvas(bitmap);
my_canvas .drawColor(Color.BLACK);
//然后在调用onDraw()方法将bitmap画上去
canvas.drawBitmap(bitmap, 0, 0, null);
1.1创建Bitmap方法一:BitmapFactory
我第一次用这个方法的时候是用在创建一个和图片一样的画布的时候。实现对图片裁剪的效果。方法如下:
Bitmap mImageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.card_view_image);
//获取图片宽高度
mWidth = mImageBitmap.getWidth();
mHight = mImageBitmap.getHeight();
//创建一个和上面图片一样的一个画布
mMaskBitmap = Bitmap.createBitmap(mWidth, mHight, Bitmap.Config.ARGB_8888);
Canvas maskCanvas = new Canvas(mMaskBitmap);
BitmapFactory用于从各种资源,文件,数据流和字节数组中创建Bitmap(位图)对象。BitmapFactory类是一个工具类,提供了大量函数,这些函数可用于从不同的数据源中解析、创建Bitmap(位图)对象。
BitmapFactory所有的函数:
//多用来操作本地的Drawable资源文件
public static Bitmap decodeResource(Resource res,int id);
public static Bitmap decodeResource(Resource res,int id,Options opts);
//通过文件路径加载图片
public static Bitmap decodeFile(String pathName);
public static Bitmap decodeFile(String pathName,Options opts);
public static Bitmap decodeFileDescriptor(FileDescriptor fd);
public static Bitmap decodeFileDescriptor(FileDescriptor fd,Rect outPadding,Options opts);
//根据Byte数组解析出Bitmap data:压缩图像数据的字节组 offect:图像的偏移量,用于解码器定位从哪开始解析 length:字节数,从偏移量开始指定多少字节来解析
public static Bitmap decodeByteArray(byte[] data,int offset,int length);
public static Bitmap decodeByteArray(byte[] data,int offset,int length,Options opts);
public static Bitmap decodeStream(InputStream is);
public static Bitmap decodeStream(InputStream is,Rect outPadding,Options opts);
public static Bitmap decodeResourceStream(Resource res,TypedValue value,InputStream is,Rect pad,Options opts);
1.decodeResource(Resource res,int id)
这个函数表示从资源中解码一张位图,主要以R.drawable.***形式从本地资源中加载。
参数:
- Resource res:包含图像数据的资源对象,一般通过Context.getResource()函数获得。
- int id:包含图像数据的资源id
2.decodeByteArray(byte[] data,int offset,int length)
参数:
- 根据Byte数组解析出Bitmap
- data:压缩图像数据的字节组 offect:图像的偏移量,用于解码器定位从哪开始解析
- length:字节数,从偏移量开始指定多少字节来解析
使用步骤一般如下:
(1)开启异步线程无获取网络图片。
(2)网络返回InputStream。
(3)把InputStream转换成byte[]。
(4)解析:Bitmap bm = BitmapFactory.decodeByteArray(myByte,0,myByte.length)。
伪代码如下:
Imageview iv = findViewById(R.id.img);
new Thread(new Runnable(){
@override
public void run(){
try{
byte[] data = getImage(path);
int length = data.length;
final Bitmap bitMap = BitmapFactory.decodeByteArray(data,0,legth);
iv.post(new Runnable(){
@override
public void run(){
iv.setImageBitmap(bitmap);
}
});
}
}
}).start();
public byte[] getImage(String path) throws Exception{
URL url = new URL(path);
HttpURLConnetion httpURLconnection = (HttpURLConnetion)url.openConnection();
httpURLconnection.setRequestMethod("Get");
httpURLconnection.setReadTimeout(6*1000);
InputStream in = null;
if()httpURLconnection.getResponseCode() == 200){
in = httpURLconnection.getInputStream();
byte[] result = readStream(in);
in.close();
return result;
}
return null;
}
public byte[] readStream(InputStream in) throws Expection{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = in.read(buffer))!= -1){
outputStream.write(buffer,0,len);
}
outputStream.close();
in.close();
return outputStream.toByteArray();
}
1.2创建Bitmap方法二:Bitmap静态方法(createBitmap)
成果展示
1.自定义View_菱形图片
public class CustomDiamonImage extends View {
private Paint mPaint;
private int mWidth;
private int mHight;
private Bitmap mMaskBitmap;
private Bitmap mImageBitmap;
public CustomDiamonImage(Context context) {
super(context);
}
public CustomDiamonImage(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(getBitmap(), 0, 0, null);
}
private Bitmap getBitmap() {
mImageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.card_view_image);
//获取图片宽高度
mWidth = mImageBitmap.getWidth();
mHight = mImageBitmap.getHeight();
//好像是创建一个和上面图片一样的一个图层
mMaskBitmap = Bitmap.createBitmap(mWidth, mHight, Bitmap.Config.ARGB_8888);
Canvas maskCanvas = new Canvas(mMaskBitmap);
mPaint = new Paint();
mPaint.setColor(Color.YELLOW);
mPaint.setAntiAlias(true);//设置抗锯齿
//将画布先平移后旋转
maskCanvas.translate(mWidth/2,0);
maskCanvas.rotate(45);
int rectSize = (int)(mWidth/2/Math.sin(Math.toRadians(45)));
//绘制圆角矩形
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
maskCanvas.drawRoundRect(0,0,rectSize,rectSize,50,50,mPaint);
}
//设置混合显示模式为src_in
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//画布先旋转后平移,防止图片也跟着旋转
maskCanvas.rotate(-45);
maskCanvas.translate(-mWidth/2,0);
maskCanvas.drawBitmap(mImageBitmap,0,0,mPaint);
return mMaskBitmap;
}
}