android – 具有中风的圆角的位图图像

前端之家收集整理的这篇文章主要介绍了android – 具有中风的圆角的位图图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个锐利边缘的图像.

tile_mode.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:tileMode="repeat">
</bitmap>@H_301_5@ 
 

back.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
       <item android:drawable="@drawable/tile_mode" />
    <item>
        <shape>
            <solid/>
            <stroke android:width="1dip" android:color="#225786" />
            <corners android:radius="10dip"/>
            <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
        </shape>
    </item>@H_301_5@ 
 

layout.xml

<LinearLayout
                android:id="@+id/frame1"
                android:background="@drawable/back"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </LinearLayout>@H_301_5@ 
 

我将图像设置为此布局的背景,并绘制边框,但问题是图像是正方形的,边缘锐利,而我在xml中绘制的边框是圆角.那么如何使图像也有圆角?

解决方法

这是一个解决方案,你必须圆形到你的主要布局背景和内部保持您的图像与您的愿望图像:

如下所示:

back.xml

这将使您的形象圆角

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
     <stroke android:width="1dp" android:color="#dd7b7a"/>
     <corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" 
     android:topLeftRadius="10dp" android:topRightRadius="10dp"/> 
     <solid android:color="#dd7b7a"/>
 </shape>@H_301_5@ 
 

tile_mode.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:tileMode="repeat" />@H_301_5@ 
 

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
<LinearLayout 
     android:padding="4dip"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/back"
    android:gravity="center_horizontal"
    >
<LinearLayout  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
   android:background="@drawable/tile_mode"
    />
</LinearLayout>  
</LinearLayout>@H_301_5@ 
 

更新

挖了很多东西后,我碰到了已经在stackoverflow上发布的解决方

Changing Image as Rounded Corner

How to make an ImageView to have rounded corners

步骤1@

main.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"
        android:gravity="center"
        tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"

          />

    </RelativeLayout>@H_301_5@ 
 

第2步@

做一个功能,使用画布四舍五入到你的位图.

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap
                .getHeight(),Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0,bitmap.getWidth(),bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0,0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF,roundPx,paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap,rect,paint);

        return output;
    }@H_301_5@ 
 

步骤3 @

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView image=(ImageView)findViewById(R.id.image);


        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.testing);


    image.setImageBitmap(getRoundedCornerBitmap(bitmap,20));@H_301_5@
原文链接:https://www.f2er.com/android/311738.html

猜你在找的Android相关文章