【问题标题】:Rounded scaled ImageView from right side only仅从右侧圆形缩放的 ImageView
【发布时间】:2017-05-11 18:01:34
【问题描述】:

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

这就是我想要做的。

我发现那个类可以做类似的事情(TopRoundedImage,感谢作者),但对于左上角和右上角。我正在尝试更改它,但我想我仍然做错了。

另外,我正在使用 Glide 从 Internet 请求图像,然后我只想圆右侧。

我尝试使用背景可绘制对象,但它不起作用(我读它是因为规模)。

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();
    }

}

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

【问题讨论】:

  • 您可以发布您正在寻找的示例图片吗?
  • @Lingeshwaran 请检查图片

标签: android imageview


【解决方案1】:

您可以为此使用可绘制形状:

<?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

【讨论】:

  • 好的,链接有效,我不知道如何调整我的班级。如果您使用的是缩放图像,我建议使用该链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多