先看看效果图:
实现思路:擦除图片相应的角,然后层叠图片,产生倾斜效果
代码实现:
1、定义属性
在values文件夹下的attrs文件添加以下代码
<resources> <declare-styleable name="TiltVIEw"> <attr name="type" format="integer" /> </declare-styleable></resources>
2、自定义布局
public class TiltVIEw extends ImageVIEw { private int imageWIDth;//图片宽度 private int imageHeight;//图片高度 private double angle = 10 * Math.PI / 180;//三角形角度 private int triangleHeight;//三角形高度 private Paint paint;//画笔 private Path path;//绘制路径 private int type;//倾斜图片的类型 public TiltVIEw(Context context) { this(context,null); } public TiltVIEw(Context context,AttributeSet attrs) { this(context,attrs,0); } public TiltVIEw(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.TiltVIEw); type = array.getInteger(R.styleable.TiltVIEw_type,1); array.recycle(); } //重测大小 @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { 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(int measureSpec) { int minLength = 200; int mode = MeasureSpec.getMode(measureSpec); int length = MeasureSpec.getSize(measureSpec); if (mode == MeasureSpec.AT_MOST) { length = Math.min(length,minLength); } return length; } @OverrIDe protected voID onDraw(Canvas canvas) { initPaint(); Bitmap mBitmap = Bitmap.createBitmap(imageWIDth,imageHeight,Bitmap.Config.ARGB_8888); //初始化Bitmap Canvas mCanvas = new Canvas(mBitmap);//创建画布,并绘制mBitmap Bitmap mBackBitmap = ((BitmapDrawable) getDrawable()).getBitmap(); mCanvas.drawBitmap(resizeBitmap(mBackBitmap),null);//绘制Bitmap setTriangle(); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); mCanvas.drawPath(path,paint); canvas.drawBitmap(mBitmap,null); } //初始化画笔 private voID initPaint() { paint = new Paint(); 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 = new Path(); switch (type) { case 1://右下角 path.moveto(0,imageHeight); path.lineto(imageWIDth,imageHeight - triangleHeight); path.lineto(0,imageHeight); break; case 2://左上角+左下角 path.moveto(0,triangleHeight); path.lineto(imageWIDth,0); path.lineto(0,imageHeight); path.lineto(0,imageHeight - triangleHeight); break; case 3://右上角+右下角 path.moveto(imageWIDth,triangleHeight); path.lineto(0,0); path.lineto(imageWIDth,imageHeight - triangleHeight); break; case 4://右上角 path.moveto(0,0); break; case 5://左上角 path.moveto(0,0); break; } } //重新调节图片大小 private Bitmap resizeBitmap(Bitmap bitmap) { int wIDth = bitmap.getWIDth(); int height = bitmap.getHeight(); // 设置想要的大小 int newWIDth = imageWIDth; int newHeight = imageHeight; // 计算缩放比例 float scaleWIDth = ((float) newWIDth) / wIDth; float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postscale(scaleWIDth,scaleHeight); // 得到新的图片 return Bitmap.createBitmap(bitmap,wIDth,height,matrix,true); }}
3、布局代码调用
//其中androID:layout_margintop="-15dp"对效果实现有很大的作用<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <com.pengkv.may.Widget.TiltVIEw androID:layout_wIDth="match_parent" androID:layout_height="100dp" androID:src="@drawable/sample_0" app:type="1" /> <com.pengkv.may.Widget.TiltVIEw androID:layout_wIDth="match_parent" androID:layout_height="100dp" androID:layout_margintop="-15dp" androID:src="@drawable/sample_1" app:type="2" /> <com.pengkv.may.Widget.TiltVIEw androID:layout_wIDth="match_parent" androID:layout_height="100dp" androID:layout_margintop="-15dp" androID:src="@drawable/sample_2" app:type="4" /></linearLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的最近较流行的效果 Android自定义View实现倾斜列表/图片全部内容,希望文章能够帮你解决最近较流行的效果 Android自定义View实现倾斜列表/图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)