ImageView的子类,使圆角右角 [英] ImageView subclass to make Rounded Right Corner

查看:156
本文介绍了ImageView的子类,使圆角右角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采取这样的图片

和把它们变成图像右上方的1/4。下面的图片是什么,我需要一个例子。

*的更新:*

我把它(在某种程度上)工作,但缩放图像产生失真的量不能接受我的code似乎非常低效的。关于如何提高这个code或A可能更好的办法任何想法?

我继承凌空的NetworkImageView。

下面是我的code

 < com.RoundedNetworkImageView
    机器人:ID =@ + ID / smallProductImage
    机器人:layout_width =56dp
    机器人:layout_height =56dp
    机器人:layout_centerVertical =真
    机器人:layout_margin =5DP
    机器人:adjustViewBounds =真
    机器人:背景=@机器人:彩色/白
    机器人:scaleType =fitCenter/>公共静态位图getCroppedBitmap(BMP位图,诠释半径){
    位图sbmp;    如果(bmp.getWidth()!=半径|| bmp.getHeight()!=半径){
        浮最小= Math.min(bmp.getWidth(),bmp.getHeight());
        浮动系数=最小/半径;
        sbmp = Bitmap.createScaledBitmap(BMP,(INT)(bmp.getWidth()/因子),(INT)(bmp.getHeight()/因子)
                假);
    }其他{
        sbmp = BMP;
    }    位图输出= Bitmap.createBitmap(半径,半径,Config.ARGB_8888);
    帆布帆布=新的Canvas(输出);    最终诠释颜色= 0xffa19774;
    最终涂料粉刷=新的油漆();
    最终矩形矩形=新的Rect(0,0,半径,半径);    paint.setAntiAlias​​(真);
    paint.setFilterBitmap(真);
    paint.setDither(真);
    canvas.drawARGB(0,0,0,0);
    paint.setColor(Color.parseColor(#BAB399));
    canvas.drawCircle(半径/ 2 + 0.7f,半径/ 2 + 0.7f,半径/ 2 + 0.1F,油漆);
    paint.setXfermode(新PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp,矩形,矩形,油漆);    位图的位图= Bitmap.createBitmap(输出,output.getWidth()/ 2,0,output.getWidth()/ 2,
            output.getHeight()/ 2);
    位图scaledBmp = Bitmap.createScaledBitmap(位图,半径,半径,真正的);    返回scaledBmp;
}


解决方案

如何对未来图书馆:

https://github.com/vinc3m1/RoundedImageView

I am trying to take an image like these

and turn them into the top right 1/4 of the image. The image below is an example of what I need.

*UPDATE: *

I have it (sort of) working, but scaling the image creates an unacceptable amount of distortion my code seems terribly inefficient. Any ideas on how to improve this code or a possible better approach?

I am subclassing Volley's NetworkImageView.

Here is my code

<com.RoundedNetworkImageView
    android:id="@+id/smallProductImage"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_centerVertical="true"
    android:layout_margin="5dp"
    android:adjustViewBounds="true"
    android:background="@android:color/white "
    android:scaleType="fitCenter" />

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;

    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
        float factor = smallest / radius;
        sbmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() / factor), (int) (bmp.getHeight() / factor),
                false);
    } else {
        sbmp = bmp;
    }

    Bitmap output = Bitmap.createBitmap(radius, radius, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, radius, radius);

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(radius / 2 + 0.7f, radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    Bitmap bitmap = Bitmap.createBitmap(output, output.getWidth() / 2, 0, output.getWidth() / 2,
            output.getHeight() / 2);
    Bitmap scaledBmp = Bitmap.createScaledBitmap(bitmap, radius, radius, true);

    return scaledBmp;
}

解决方案

How about the next library:

https://github.com/vinc3m1/RoundedImageView

?

这篇关于ImageView的子类,使圆角右角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆