android 平铺图片 类似word里面平铺 像ios里面的平铺效果
需要自定义一个view (新建class 继承View)
package com.example.myapplication1.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;
import com.example.myapplication1.R;
/**
* @ClassName BitmapShaderView
* @Description TODO
* @Author ZhangXueTao
* @Date 2020/10/9 8:44
* @Version 1.0 模板 29
*/
public class BitmapShaderView extends View {
private Bitmap mBitmap = null;
private ShapeDrawable sDrawable = null;
private Shader mBitmapShader = null;
public BitmapShaderView(Context context) {
this(context, null);
}
public BitmapShaderView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BitmapShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init() {
mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pm292pingguo);
mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Color.BLUE, Color.WHITE}, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
sDrawable = new ShapeDrawable(new OvalShape());
sDrawable.getPaint().setShader(mBitmapShader);
sDrawable.setBounds(0, 0, 1000, 500);
sDrawable.draw(canvas);
}
}
1.其中在init()方法里面出现了Shader.TileMode.REPEAT 解释一下构造方法详解
BitmapShader(图像渲染)
BitmapShader(Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY)
使用一张位图作为纹理来对某一区域进行填充,参数依次:
- bitmap:用来作为填充的位图;
- tileX:X轴方向上位图的衔接形式;
- tileY:Y轴方向上位图的衔接形式;
而这个Shader.TileMode有三种:
- CLAMP:就是如果渲染器超出原始边界范围,则会复制边缘颜色对超出范围的区域进行着色
- REPEAT:则是平铺形式重复渲染
- MIRROR:则是在横向和纵向上以镜像的方式重复渲染位图。
2.在onDraw()方法里面出现了sDrawable = new ShapeDrawable(new OvalShape());
-
画圆形 OvalShape
-
画半圆 ArcShape
-
画矩形 RectShape
-
画内外双层矩形,并且有圆角 RoundRectShape
-
画任意形状 PathShape

浙公网安备 33010602011771号