仅从右侧舍入缩放的ImageView

时间:2017-05-11 08:02:24

标签: android imageview

如何仅从右上角和右下角(右侧)舍入缩放的图像(CenterCrop)。

Sample这就是我想要做的。

我发现可以做类似的类(TopRoundedImage,感谢作者),但是左上角和右上角。我试图改变它,但我想我仍然做错了。

另外,我正在使用Glide从互联网上请求图像,然后我想只在右侧进行。

我曾尝试使用背景绘图,但它不起作用(我读它是因为比例)。

public class RightRoundedImageView extends AppCompatImageView
{
    private final RectF roundRect = new RectF();
    private float rect_radius = 7;
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();

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

    public RightRoundedImageView(Context context)
    {
        super(context);
        init();
    }

    private void init()
    {
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
        float density = getResources().getDisplayMetrics().density;
        rect_radius = rect_radius * density;
    }

    public void setRectAdius(float radius)
    {
        rect_radius = radius;
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);
        int w = getWidth();
        int h = getHeight();
//        roundRect.set(0, 0, w, h + rect_radius);//round top
        roundRect.set(0, 0, w + rect_radius, h); //round left
//        roundRect.set(0, 0, w - rect_radius, h); //round all


    }

    @Override
    public void draw(Canvas canvas)
    {
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawRoundRect(roundRect, rect_radius, rect_radius, zonePaint);
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
    }

}

如果有人知道或可以提供帮助,我们将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

你可以使用drawable形状:

<?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#FFFFFF"/>
        <stroke android:width="3dip" android:color="#B1BCBE" />
        <corners android:bottomLeftRadius="0dp" android:bottomRightRadius="10dp"
            android:topLeftRadius="0dp" android:topRightRadius="10dp"/>
        <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
    </shape>

imageView.setBackground(this.getResources().getDrawable(R.drawable.my_shape));

另请查看此链接问题how-to-make-an-imageview-with-rounded-corners