自定义斜边的imageview

最近要做一个斜边的imageview,就自己写了下,做个笔记记下来


在values文件夹下的attrs文件添加以下代码
<resources>
<declare-styleable name="TiltView">
<attr name="type" format="integer" />
</declare-styleable>
</resources>

在布局中

<com.example.admin.myapplication.TitledImageView
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:src="@mipmap/test1"
    android:scaleType="fitCenter"
    app:type="1"
    />
<com.example.admin.myapplication.TitledImageView
    android:layout_marginTop="10dp"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:src="@mipmap/test1"
    android:scaleType="fitCenter"
    app:type="2"
    />


自定义view代码

public classTitledImageViewextendsImageView {

private int imageWidth;//图片宽度
private int imageHeight;//图片高度
private double angle=10* Math.PI/180;//三角形角度
private int triangleHeight;//三角形高度
privatePaintpaint;//画笔
privatePathpath;//绘制路径
private int type;//倾斜图片的类型

publicTitledImageView(Context context) {
this(context,null);
}

publicTitledImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

publicTitledImageView(Context context, AttributeSet attrs,intdefStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TitledImageView);
type= array.getInteger(R.styleable.TitledImageView_type,1);
array.recycle();
}


//重测大小
@Override
protected void onMeasure(intwidthMeasureSpec,intheightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
imageWidth= measureSpec(widthMeasureSpec);
imageHeight= measureSpec(heightMeasureSpec);
setMeasuredDimension(imageWidth,imageHeight);//设置View的大小
triangleHeight= (int) (Math.abs(Math.tan(angle) * imageHeight));
}

//测量长度
private int measureSpec(intmeasureSpec) {
intminLength =200;
intmode = MeasureSpec.getMode(measureSpec);
intlength = MeasureSpec.getSize(measureSpec);

if(mode == MeasureSpec.AT_MOST) {
length = Math.min(length, minLength);
}
returnlength;
}

@Override
protected void onDraw(Canvas canvas) {
initPaint();

Bitmap mBitmap = Bitmap.createBitmap(imageWidth,imageHeight, Bitmap.Config.RGB_565);//初始化Bitmap
Canvas mCanvas = newCanvas(mBitmap);//创建画布,并绘制mBitmap
Bitmap mBackBitmap = ((BitmapDrawable) getDrawable()).getBitmap();
mCanvas.drawBitmap(resizeBitmap(mBackBitmap),0,0,null);//绘制Bitmap

setTriangle();
/*paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mCanvas.drawPath(path, paint);*/
canvas.clipPath(path, Region.Op.REPLACE);
canvas.drawBitmap(mBitmap,0,0,null);

canvas.restore();
}

//初始化画笔
private void initPaint() {
paint=newPaint();
paint.setDither(true);//设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰
paint.setAntiAlias(true);//设置抗锯齿
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);//圆角
}


//设置三角形区域
private void setTriangle() {
path=newPath();
switch(type) {
case1://右下角
path.moveTo(0,0);
path.lineTo(0,imageHeight);
path.lineTo(imageWidth-triangleHeight,imageHeight);
path.lineTo(imageWidth,0);
break;
case2://右上角
path.moveTo(0,0);
path.lineTo(0,imageHeight);
path.lineTo(imageWidth,imageHeight);
path.lineTo(imageWidth-triangleHeight,0);
break;
}
}

//重新调节图片大小
privateBitmap resizeBitmap(Bitmap bitmap) {
intwidth = bitmap.getWidth();
intheight = bitmap.getHeight();
//设置想要的大小
intnewWidth =imageWidth;
intnewHeight =imageHeight;
//计算缩放比例
floatscaleWidth = ((float) newWidth) / width;
floatscaleHeight = ((float) newHeight) / height;
//取得想要缩放的matrix参数
Matrix matrix = newMatrix();
matrix.postScale(scaleWidth, scaleHeight);
//得到新的图片
returnBitmap.createBitmap(bitmap,0,0, width, height, matrix,true);
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值