【问题标题】:Set image with rounded corners into ImageView将带圆角的图像设置为 ImageView
【发布时间】:2014-07-19 10:30:48
【问题描述】:

我有一个ListView,上面有联系人图片和姓名或号码。现在我想把这个联系人图像的角落弄圆。

public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;
    Log.d("main", "pos:" + "" + position);

            if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new UserHolder();
        holder.Name = (TextView) row.findViewById(R.id.name);

        holder.Number = (TextView) row.findViewById(R.id.number);

        holder.img=(ImageView) row.findViewById(R.id.image);


        Typeface face=Typeface.createFromAsset(context.getAssets(),"helve.ttf");

        holder.Name.setTypeface(face);
        //holder.Number.setTypeface(face);

        //bit = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
       //

        //holder.img.setImageBitmap(getRoundedCornerBitmap(bit, 40));

        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();
    }
    User user = data.get(position); 

    holder.Name.setText(user.getName());
    holder.img.setImageBitmap(user.getbi());
    //holder.img.setImageBitmap(roundCornerImage(BitmapFactory.decodeResource(user.getbi(), R.drawable.ic_launcher),60));
    holder.Number.setText(user.getNumber());




    // Give Different Back Ground To List View---------------------------------------------

    if ((position % 2) == 0) {
        row.setBackgroundResource(R.drawable.list_dark);
    } else {
        row.setBackgroundResource(R.drawable.list_light);
    }




    Log.d("main", "pos:" + "" + position);

    return row;

}



static class UserHolder {
    TextView Name,Number;
    ImageView img;

}

如何为这张图片添加圆角?我有点困惑我必须做什么样的改变。

【问题讨论】:

    标签: android performance android-imageview android-bitmap


    【解决方案1】:

    您好,请使用此自定义图像视图

    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);
    }
    
    @SuppressLint("Instantiatable")
    public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected 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);
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    
    }
    
    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
        Bitmap sbmp;
        if (bmp.getWidth() != radius || bmp.getHeight() != radius)
            sbmp = Bitmap.createScaledBitmap(bmp, radius, 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, 0, sbmp.getWidth(), sbmp.getHeight());
    
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 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, rect, paint);
    
        return output;
    }
    

    }

    【讨论】:

    • 它的错误在行... row.findViewById(R.id.image);
    • @user3855491 请替换 holder.img=(ImageView) row.findViewById(R.id.image); with holder.img=(RoundedImageView) row.findViewById(R.id.image);和 ImageView img with RoundedImageView=img in viewholder
    • 当我滚动列表而不是应用程序停止。请问有什么解决办法..??
    • 哦.. 非常感谢兄弟。
    【解决方案2】:
    If you are trying to make your imageview as rounded corner
    

    You can achieve this multiple ways

    方法一:: 使用圆形背景的drawable(可以使用九个补丁)

    方法2::你可以使用这些链接


    Here is an demo answer Source>

    如果图像周围有一些空间,请使用 XML:

    创建一个带有透明内容的边框形状,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners 
            android:radius="30dp" />
        <stroke 
            android:color="#ffffffff"
            android:width="10dp" />
    </shape> 
    

    然后在RelativeLayout 中,您可以先放置图像,然后在形状上方与另一个ImageView 相同的位置。封面形状的大小应该比边框宽度大。请注意在定义外半径时采用较大的圆角半径,但内半径是覆盖图像的部分。

    希望它也能帮助别人。

    编辑根据 CQM 请求相关布局示例:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/imageToShow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imgCorners"
            android:layout_alignLeft="@+id/imgCorners"
            android:layout_alignRight="@+id/imgCorners"
            android:layout_alignTop="@+id/imgCorners"
            android:background="#ffffff"
            android:contentDescription="@string/desc"
            android:padding="5dp"
            android:scaleType="centerCrop" />
    
        <ImageView
            android:id="@+id/imgCorners"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@string/desc"
            android:src="@drawable/corners_white" />
    
    </RelativeLayout>
    

    希望对您有所帮助

    【讨论】:

    • 实际上我不知道我在 imageview 的代码中有什么变化。
    • @user3855491 .... 如果您是 android 新手.....查看本教程 (mkyong.com/android/android-imageview-example) .... 如果您需要任何帮助,请告诉我... 快乐编码!
    【解决方案3】:

    老兄这是解决方案..

    https://github.com/MostafaGazar/CustomShapeImageView更多详情https://coderwall.com/p/hmzf4w

    使用这个库你会在这个库中找到圆形,也很容易使用。

    你也可以为这个库制作你的自定义形状,比如云等。这个库支持 SVG 文件格式,他们制作了圆角 Imageview 的 SVG 文件。

    只要使用,如果有任何问题,请告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      相关资源
      最近更新 更多