android – 在圆形imageview中显示时图像变得拉伸

android – 在圆形imageview中显示时图像变得拉伸,第1张

概述下面是我的扩展 ImageView的RoundedImageView类: public class RoundedImageView extends ImageView {public RoundedImageView(Context context) { super(context); // TODO Auto-generated constructor stub}pu 下面是我的扩展 ImageVIEw的RoundedImageVIEw类:
public class RoundedImageVIEw extends ImageVIEw {public RoundedImageVIEw(Context context) {    super(context);    // Todo auto-generated constructor stub}public RoundedImageVIEw(Context context,AttributeSet attrs) {    super(context,attrs);}public RoundedImageVIEw(Context context,AttributeSet attrs,int defStyle) {    super(context,attrs,defStyle);}@OverrIDeprotected voID onDraw(Canvas canvas) {    Drawable drawable = getDrawable();    if (drawable == null) {        return;    }    if (getWIDth() == 0 || getHeight() == 0) {        return;     }    Bitmap b =  ((BitmapDrawable)drawable).getBitmap() ;    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888,true);    int w = getWIDth(),h = getHeight();    Bitmap roundBitmap =  getCroppedBitmap(bitmap,w);   // roundBitmap= ImageUtils.setCircularInnerGlow(roundBitmap,0xFFBAB399,4,1);    canvas.drawBitmap(roundBitmap,null);}public static Bitmap getCroppedBitmap(Bitmap bmp,int radius) {    Bitmap sbmp;    if(bmp.getWIDth() != radius || bmp.getHeight() != radius)        sbmp = Bitmap.createScaledBitmap(bmp,radius,false);    else        sbmp = bmp;    Bitmap output = Bitmap.createBitmap(sbmp.getWIDth(),sbmp.getHeight(),Config.ARGB_8888);    Canvas canvas = new Canvas(output);    final int color = 0xffa19774;    final Paint paint = new Paint();    final Rect rect = new Rect(0,sbmp.getWIDth(),sbmp.getHeight());    paint.setAntiAlias(true);    paint.setFilterBitmap(true);    paint.setDither(true);    canvas.drawARGB(0,0);    paint.setcolor(color.parsecolor("#BAB399"));    canvas.drawCircle(sbmp.getWIDth() / 2+0.7f,sbmp.getHeight() / 2+0.7f,sbmp.getWIDth() / 2+0.1f,paint);    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));    canvas.drawBitmap(sbmp,rect,paint);            return output;}

我的xml代码是:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" >    <linearLayout        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_centerInParent="true"        androID:orIEntation="horizontal" >        <com.example.scaletypedemo.RoundedImageVIEw            androID:layout_marginleft="30dp"            androID:layout_wIDth="100dp"            androID:layout_height="100dp"            androID:layout_centerInParent="true"            androID:scaleType="centerCrop"            androID:src="@drawable/a"            androID:adjustVIEwBounds="true" />         <ImageVIEw                androID:layout_marginleft="50dp"                androID:layout_wIDth="100dp"                androID:layout_height="100dp"                androID:scaleType="centerCrop"                androID:src="@drawable/a" />    </linearLayout></relativeLayout>

所以,问题是,如果我使用ImageVIEw,显示图像不会被拉伸但是如果我使用相同的图像并将其显示在RoundedImageVIEw中并保持所有属性相同;图像被拉伸,如下面的屏幕截图所示:

请帮助防止RoundedImageVIEw中显示的图像拉伸………..我在这一点上非常困难.

编辑:应用正红的解决方案后,它解决了拉伸问题,但后来没有显示完整的一轮.

提前致谢!!!

解决方法 您应该按宽度/高度速率调整位图大小.

—- —- EDITED
这将是一种更灵活的方式.

@OverrIDeprotected voID onDraw(Canvas canvas) {    Drawable drawable = getDrawable();    if (drawable == null) {        return;    }    if (getWIDth() == 0 || getHeight() == 0) {        return;    }    Bitmap b = ((BitmapDrawable) drawable).getBitmap();    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888,h = getHeight();    int radius = w < h ? w : h;    Bitmap roundBitmap = getCroppedBitmap(bitmap,w,h);    // roundBitmap= ImageUtils.setCircularInnerGlow(roundBitmap,// 4,int radius,int w,int h) {    Bitmap sbmp;    if (bmp.getWIDth() != radius || bmp.getHeight() != radius) {        float _w_rate = 1.0f * radius / bmp.getWIDth();        float _h_rate = 1.0f * radius / bmp.getHeight();        float _rate = _w_rate < _h_rate ? _h_rate : _w_rate;        sbmp = Bitmap.createScaledBitmap(bmp,(int)(bmp.getWIDth() * _rate),(int)(bmp.getHeight() * _rate),false);    }    else        sbmp = bmp;    Bitmap output = Bitmap.createBitmap(sbmp.getWIDth(),0);    paint.setcolor(color.parsecolor("#BAB399"));    canvas.drawCircle(w / 2,h / 2,(w < h ? w : h) / 2,paint);    return output;}
总结

以上是内存溢出为你收集整理的android – 在圆形imageview中显示时图像变得拉伸全部内容,希望文章能够帮你解决android – 在圆形imageview中显示时图像变得拉伸所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1137475.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存