首页 > 其他分享 >对视图的对角线切割DiagonalView

对视图的对角线切割DiagonalView

时间:2023-05-01 16:10:58浏览次数:54  
标签:import gravity 视图 height width DiagonalView 对角线 path public


提供对视图的对角线切割,具有很好的用户定制



对视图的对角线切割DiagonalView_ide



对视图的对角线切割DiagonalView_xml_02



基本用法:

<com.intrusoft.squint.DiagonalView
                android:id="@+id/diagonal"
                android:layout_width="match_parent"
                android:layout_height="240dp"
                android:scaleType="centerCrop"
                android:src="@drawable/c1"
                squint:diagonalDirection="bottom_to_top"
                squint:angle="12"
                squint:gravity="right" />



所有属性:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="src" format="reference" />
    <attr name="angle" format="integer" />
    <attr name="scaleType">
        <enum name="centerCrop" value="0" />
        <enum name="fitXY" value="1" />
    </attr>
    <attr name="gravity">
        <enum name="left" value="0" />
        <enum name="right" value="1" />
        <enum name="top" value="2" />
        <enum name="bottom" value="3" />
    </attr>

    <attr name="diagonalDirection">
        <enum name="left_to_right" value="0" />
        <enum name="right_to_left" value="1" />
        <enum name="top_to_bottom" value="2" />
        <enum name="bottom_to_top" value="3" />
    </attr>

    <attr name="tint" format="color" />
    <attr name="solidColor" format="color" />
    <attr name="fillColor" format="color" />

    <declare-styleable name="DiagonalView">
        <attr name="angle" />
        <attr name="gravity" />
        <attr name="tint" />
        <attr name="solidColor" />
        <attr name="fillColor" />
        <attr name="diagonalDirection" />
    </declare-styleable>
</resources>




也可以通过代码设置:


DiagonalView diagonalView = (DiagonalView) findViewById(R.id.diagonal);

// to set image from resources        
diagonalView.setImageSource(R.drawable.your_image);

// to set bitmap
diagonalView.setBitmap(bitmap);

// to set the diagonal angle
diagonalView.setAngle(15);

// to set the diagonal gravity
diagonalView.setGravity(DiagonalView.Gravity.LEFT);

// to set the background color (color should have some alpha val)
diagonalView.setColorTint(Color.GREEN);

// to make the solid color diagonal
diagonalView.setSolidColor(Color.BLUE);




一共有三个类,只供学习用:


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.intrusoft.squint.Squint.Direction;
import com.intrusoft.squint.Squint.Gravity;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;
import static com.intrusoft.squint.Squint.DIRECTIONS;
import static com.intrusoft.squint.Squint.GRAVITIES;

public class DiagonalView extends ImageView {

    private final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
    private final Paint fillPaint = new Paint(ANTI_ALIAS_FLAG);
    private final Paint tintPaint = new Paint(ANTI_ALIAS_FLAG);
    private final Paint bitmapPaint = new Paint(ANTI_ALIAS_FLAG);
    private final Paint solidPaint = new Paint(ANTI_ALIAS_FLAG);
    private final Matrix shaderMatrix = new Matrix();
    private final int DEFAULT_ALPHA = 50;
    private static int tintColor = Color.TRANSPARENT;
    private static int fillColor = Color.TRANSPARENT;
    private static int solidColor = Color.TRANSPARENT;
    private float width;
    private float height;
    private int angle = 10;
    private Gravity gravity = Gravity.BOTTOM;
    private Direction direction = Direction.LEFT_TO_RIGHT;
    private Bitmap bitmap;
    private BitmapShader bitmapShader;
    private ColorFilter colorFilter;


    public DiagonalView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public DiagonalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public DiagonalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    private void init(Context context, AttributeSet attributeSet, int defStyleAttr) {
        if (attributeSet != null) {
            TypedArray array = context.obtainStyledAttributes(attributeSet, R.styleable.DiagonalView, defStyleAttr, 0);
            angle = array.getInt(R.styleable.DiagonalView_angle, angle);
            gravity = GRAVITIES[array.getInt(R.styleable.DiagonalView_gravity, 3)];
            tintColor = array.getColor(R.styleable.DiagonalView_tint, Color.TRANSPARENT);
            fillColor = array.getColor(R.styleable.DiagonalView_fillColor, Color.TRANSPARENT);
            solidColor = array.getColor(R.styleable.DiagonalView_solidColor, Color.TRANSPARENT);
            direction = DIRECTIONS[array.getInt(R.styleable.DiagonalView_diagonalDirection, 0)];
            array.recycle();
        }
        fillPaint.setStyle(Paint.Style.FILL);
        fillPaint.setColor(fillColor);
        tintPaint.setStyle(Paint.Style.FILL);
        tintPaint.setColor(tintColor);
        solidPaint.setStyle(Paint.Style.FILL);
        solidPaint.setColor(solidColor);
        solidPaint.setAlpha(255);
        if (tintPaint.getAlpha() == 255)
            tintPaint.setAlpha(DEFAULT_ALPHA);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        initializeBitmap();
    }

    @Override
    public void draw(Canvas canvas) {
        Path path = PathHelper.getPathFor(getWidth(), getHeight(), angle, direction, gravity);
        if (bitmap != null) {
            if (fillColor != Color.TRANSPARENT)
                canvas.drawPath(path, fillPaint);
            canvas.drawPath(path, bitmapPaint);
            if (tintColor != Color.TRANSPARENT)
                canvas.drawPath(path, tintPaint);
        }
        if (solidColor != Color.TRANSPARENT)
            canvas.drawPath(path, solidPaint);
    }

    private void initializeBitmap() {
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        Drawable drawable = getDrawable();
        if (drawable != null) {
            if (drawable instanceof BitmapDrawable)
                bitmap = ((BitmapDrawable) drawable).getBitmap();
            else {
                try {
                    int COLOR_DRAWABLE_DIMENSIONS = 2;
                    if (drawable instanceof ColorDrawable)
                        bitmap = Bitmap.createBitmap(COLOR_DRAWABLE_DIMENSIONS, COLOR_DRAWABLE_DIMENSIONS, BITMAP_CONFIG);
                    else
                        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);

                    Canvas canvas = new Canvas(bitmap);
                    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                    drawable.draw(canvas);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (bitmap == null) {
                invalidate();
                return;
            }
            if (bitmapPaint != null) {
                bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                bitmapPaint.setShader(bitmapShader);
            }
            if (this.getScaleType() != ScaleType.CENTER_CROP && this.getScaleType() != ScaleType.FIT_XY)
                this.setScaleType(ScaleType.CENTER_CROP);
            setUpScaleType();
            applyColorFilter();
            invalidate();
        }
    }

    private void applyColorFilter() {
        if (bitmapPaint != null) {
            bitmapPaint.setColorFilter(colorFilter);
        }
    }

    private void setUpScaleType() {
        float scaleX = 1, scaleY = 1, dx = 0, dy = 0;
        if (bitmap == null || shaderMatrix == null)
            return;
        shaderMatrix.set(null);
        if (getScaleType() == ScaleType.CENTER_CROP) {
            if (width != bitmap.getWidth()) {
                scaleX = width / bitmap.getWidth();
            }
            if (scaleX * bitmap.getHeight() < height) {
                scaleX = height / bitmap.getHeight();
            }
            dy = (height - bitmap.getHeight() * scaleX) * 0.5f;
            dx = (width - bitmap.getWidth() * scaleX) * 0.5f;
            shaderMatrix.setScale(scaleX, scaleX);
        } else {
            scaleX = width / bitmap.getWidth();
            scaleY = height / bitmap.getHeight();
            dy = (height - bitmap.getHeight() * scaleY) * 0.5f;
            dx = (width - bitmap.getWidth() * scaleX) * 0.5f;
            shaderMatrix.setScale(scaleX, scaleY);
        }
        shaderMatrix.postTranslate(dx + 0.5f, dy + 0.5f);
        bitmapShader.setLocalMatrix(shaderMatrix);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        if (colorFilter == this.colorFilter) {
            return;
        }
        this.colorFilter = colorFilter;
        applyColorFilter();
        invalidate();
    }

    @Override
    public ColorFilter getColorFilter() {
        return colorFilter;
    }

    @Override
    public void setScaleType(ScaleType scaleType) {
        if (scaleType == ScaleType.CENTER_CROP || scaleType == ScaleType.FIT_XY)
            super.setScaleType(scaleType);
        else
            throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
    }

    @Override
    public void setAdjustViewBounds(boolean adjustViewBounds) {
        if (adjustViewBounds) {
            throw new IllegalArgumentException("adjustViewBounds not supported.");
        }
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        initializeBitmap();
    }

    @Override
    public void setImageURI(Uri uri) {
        super.setImageURI(uri);
        initializeBitmap();
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        initializeBitmap();
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        initializeBitmap();
    }

    public int getTintColor() {
        return tintColor;
    }

    public void setTintColor(int tintColor) {
        DiagonalView.tintColor = tintColor;
        tintPaint.setColor(tintColor);
        if (tintPaint.getAlpha() == 255)
            tintPaint.setAlpha(DEFAULT_ALPHA);
        invalidate();
    }

    public int getFillColor() {
        return fillColor;
    }

    public void setFillColor(int fillColor) {
        DiagonalView.fillColor = fillColor;
        fillPaint.setColor(fillColor);
        invalidate();
    }

    public int getSolidColor() {
        return solidColor;
    }

    public void setSolidColor(int solidColor) {
        DiagonalView.solidColor = solidColor;
        solidPaint.setColor(solidColor);
        solidPaint.setAlpha(255);
        invalidate();
    }

    public void setDiagonalGravity(Gravity gravity) {
        this.gravity = gravity;
        invalidate();
    }

    public void setDiagonalDirection(Direction direction) {
        this.direction = direction;
        invalidate();
    }

    public void setAngle(int angle) {
        this.angle = angle;
        invalidate();
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        invalidate();
    }
}




import android.graphics.Path;

import com.intrusoft.squint.Squint.Direction;
import com.intrusoft.squint.Squint.Gravity;

import static com.intrusoft.squint.Squint.Gravity.BOTTOM;
import static com.intrusoft.squint.Squint.Gravity.LEFT;
import static com.intrusoft.squint.Squint.Gravity.RIGHT;
import static com.intrusoft.squint.Squint.Gravity.TOP;

public class PathHelper {

    public static Path getPathFor(float width, float height, double angle, Direction direction, Gravity gravity) {
        switch (direction) {
            case LEFT_TO_RIGHT:
                return leftToRight(width, height, angle, gravity);
            case RIGHT_TO_LEFT:
                return rightToLeft(width, height, angle, gravity);
            case TOP_TO_BOTTOM:
                return topToBottom(width, height, angle, gravity);
            case BOTTOM_TO_TOP:
                return bottomToTop(width, height, angle, gravity);
        }
        return null;
    }


    private static Path leftToRight(float width, float height, double angle, Gravity gravity) {
        if (gravity == LEFT || gravity == RIGHT)
            gravity = BOTTOM;
        Path path = new Path();
        float attitude = (float) Math.abs(Math.tan(Math.toRadians(angle)) * width);
        if (gravity == BOTTOM) {
            path.moveTo(0, 0);
            path.lineTo(width, 0);
            path.lineTo(width, height - attitude);
            path.lineTo(0, height);
            path.close();
        } else {
            path.moveTo(0, 0);
            path.lineTo(width, attitude);
            path.lineTo(width, height);
            path.lineTo(0, height);
            path.close();
        }
        return path;
    }

    private static Path rightToLeft(float width, float height, double angle, Gravity gravity) {
        if (gravity == LEFT || gravity == RIGHT)
            gravity = BOTTOM;
        Path path = new Path();
        float attitude = (float) Math.abs(Math.tan(Math.toRadians(angle)) * width);
        if (gravity == BOTTOM) {
            path.moveTo(0, 0);
            path.lineTo(width, 0);
            path.lineTo(width, height);
            path.lineTo(0, height - attitude);
            path.close();
        } else {
            path.moveTo(0, attitude);
            path.lineTo(width, 0);
            path.lineTo(width, height);
            path.lineTo(0, height);
            path.close();
        }
        return path;
    }

    private static Path topToBottom(float width, float height, double angle, Gravity gravity) {
        if (gravity == TOP || gravity == BOTTOM)
            gravity = LEFT;
        float attitude = (float) Math.abs(Math.tan(Math.toRadians(angle)) * height);
        Path path = new Path();
        if (gravity == LEFT) {
            path.moveTo(0, 0);
            path.lineTo(0, height);
            path.lineTo(width - attitude, height);
            path.lineTo(width, 0);
            path.close();
        } else {
            path.moveTo(0, 0);
            path.lineTo(attitude, height);
            path.lineTo(width, height);
            path.lineTo(width, 0);
            path.close();
        }
        return path;
    }

    private static Path bottomToTop(float width, float height, double angle, Gravity gravity) {
        if (gravity == TOP || gravity == BOTTOM)
            gravity = LEFT;

        float attitude = (float) Math.abs(Math.tan(Math.toRadians(angle)) * height);
        Path path = new Path();
        if (gravity == LEFT) {
            path.moveTo(0, 0);
            path.lineTo(0, height);
            path.lineTo(width, height);
            path.lineTo(width - attitude, 0);
            path.close();
        } else {
            path.moveTo(attitude, 0);
            path.lineTo(width, 0);
            path.lineTo(width, height);
            path.lineTo(0, height);
            path.close();
        }
        return path;
    }
}




public class Squint {

   public enum Gravity {
        LEFT(0),
        RIGHT(1),
        TOP(2),
        BOTTOM(3);
        final int value;

        Gravity(int value) {
            this.value = value;
        }
    }

    static final Gravity[] GRAVITIES = {Gravity.LEFT, Gravity.RIGHT, Gravity.TOP, Gravity.BOTTOM};

    public enum Direction {
        LEFT_TO_RIGHT(0),
        RIGHT_TO_LEFT(1),
        TOP_TO_BOTTOM(2),
        BOTTOM_TO_TOP(3);
        final int value;

        Direction(int value) {
            this.value = value;
        }
    }

    static final Direction[] DIRECTIONS = {Direction.LEFT_TO_RIGHT, Direction.RIGHT_TO_LEFT, Direction.TOP_TO_BOTTOM, Direction.BOTTOM_TO_TOP};
}





github:


https://github.com/IntruderShanky/Squint

  • 对视图的对角线切割DiagonalView_ide_03

  • 大小: 759.2 KB
  • 对视图的对角线切割DiagonalView_xml_04

  • 大小: 629.2 KB
  • 查看图片附件

标签:import,gravity,视图,height,width,DiagonalView,对角线,path,public
From: https://blog.51cto.com/u_5454003/6238912

相关文章

  • 快照snapshot与物化视图materializ…
    一、快照1.权限grantcreatesnapshottozjbf;2.创建create  snapshot  SP_TEST refresh    complete     startwithsysdatenextsysdate+5/(24*60*60) as           select  *  from      SELECT*FROMSP_TESTWHERE......
  • 08-react修改state数据驱动视图UI的更新【注意和vue的区别】
    //setState修改状态如果是直接修改页面不会改变使用setState修改数据才会驱动视图的改变//setState的原理:修改玩状态之后会调用render函数importReactDomfrom"react-dom"import{Component}from"react"//react状态不可变要重新创建状态,及覆盖原来的......
  • MFC-SetImageList给列表视图控件设置图像列表
     CImageList*pImageList;HBITMAPhbmp1;CBitmap*pBitmap1;HBITMAPhbmp2;CBitmap*pBitmap2;HBITMAPhbmp3;CBitmap*pBitmap3;HBITMAPhbmp4;CBitmap*pBitmap4; pImageList=newCImageList();//创建一个CImageList类的指针变量pImageList->Cr......
  • 1572. 矩阵对角线元素的和
     分析:找了一个小规律首先对角线上的数是从第一行到最后一行按顺序的在每一行上下标逐渐加1,最后总次数是矩阵的长度最重要的是,两个对角线是对称的也就是当取前面的第一个数时,后面对角线就是-1;前面取第二个时,后面就是-2然后有个细节,当行数为奇数时需要减去一个正中间的数,重......
  • EF Core 中原生SQL、存储过程、视图的使用
    包括EFCore中原型Sql的执行,包括存储过程和视图数据处理方法,同时包括参数化执行sql语句的注意事项。原生Sql查询原生sql查询使用如下两个方法进行,查询的结构只能映射到dbset关联的对象类型DBSet.FromSqlRaw()DBSet.FromSqlInterpolated()可以使用部分linq扩展方法.FromSqlR......
  • 视图层
    目录三板斧本质HttpResponse和redirect的区别JsonResponse上传文件Django视图层-FBV与CBVFBV中request对象的几个方法CBVCBV的源码剖析分析步骤:详细分析:使用操作对应知识点:三板斧本质用来处理请求的视图函数都必须返回HttpResponse对象#可以查看源码1.HttpResponse():肯定......
  • 高性能、快响应!火山引擎 ByteHouse 物化视图功能及入门介绍
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群物化视图是指将视图的计算结果存储在数据库中的一种技术。当用户执行查询时,数据库会直接从已经预计算好的结果中获取数据,而不需要重新计算视图。具体来说,物化视图是一种以表格形式存储的结果......
  • 表和视图的区别
    表和视图的区别区别:1、视图是已经编译好的sql语句。而表不是2、视图没有实际的物理记录。而表有。3、表是内容,视图是窗口4、表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能由创建的语句来修改5、表是内模式,试图是......
  • MFC-Create动态创建列表视图控件
     BOOLbb=mylist.Create(LVS_SMALLICON|WS_DLGFRAME,rect,this,10001);//动态创建列表视图控件/*参数1:DWORDdwStyle列表视图控件的风格标准样式:LVS_ALIGNLEFT显示格式是大图标或小图标时,标签......
  • Django之视图函数层 (必会三板斧 JsonResponse对象 request对象获取文件 FBV与CBV
    目录视图层之必会三板斧用来处理请求的视图函数都必须返回HttpResponse对象情况一:啥也不返回这里会报一个没有返回HttpResponse对象的错误,由此可见必须要返回一个HttpResponse对象情况二:返回HttpResponse对象点击Ctrl键查看源码可见是HttpResponse类,所以会返回一个值情......