图像没有裁剪

Goo*_*ofy 7 android image crop bitmap

我正在设置一个像这样的background图像LinearLayout:

1.back_xml:

<?xml version="1.0" encoding="UTF-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
       <item android:drawable="@drawable/back" >
        </item>
    <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> 
Run Code Online (Sandbox Code Playgroud)

2. tile.xml

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/pie_chart_background"
        android:tileMode="repeat">
    </bitmap>
Run Code Online (Sandbox Code Playgroud)

我现在设置back.xml作为一个backgroundLinearLayout它工作正常.

我需要有一个image圆角的边角以及它的边框.但我只有圆角的边框而不是图像我的代码中有什么问题我错过了什么?

这是我的图像的样子:

在此输入图像描述

Rob*_*ood 13

在你的问题花了几个小时后,我终于实现了,希望现在它会给你你想要的相同结果,请通过下面的代码告诉我它是否有效?

将适当的参数传递给下面的函数,以获得您想要的颜色边框的圆角.

public static Bitmap getRoundedCornerImage(Bitmap bitmap, int cornerDips, int borderDips, Context context) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
                context.getResources().getDisplayMetrics());
        final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,
                context.getResources().getDisplayMetrics());
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);


        paint.setAntiAlias(true);
        paint.setColor(0xFFFFFFFF);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);


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


        paint.setColor((Color.RED)); // you can change color of your border here, to other color
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth((float) borderSizePx);
        canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

        return output;
    }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

在OnCreate

public class MainActivity extends Activity {

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


    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.testing); // your desire drawable image.
    rl.setImageBitmap(getRoundedCornerImage(bitmap, 10, 10, this));

    }
Run Code Online (Sandbox Code Playgroud)

原始图像

在此输入图像描述

产量

在此输入图像描述

以下链接帮助我实现我的目标:

在Android中使用圆角的位图边框

使用圆角创建ImageView

如何使ImageView具有圆角

如何设置paint.setColor(int color)

  • 除了上面的代码,对于你的@Goofy案例,你可以拥有一个最外面的RelativeLayout,你将拥有LinearLayout和ImageView.设置imageLayout参数:`android:layout_alignTop ="@ + id/frame1"android:layout_alignBottom ="@ + id/frame1"`和LinearLayout的背景为Transparent.这样,用户就会感觉好像线性布局有背景图像集! (3认同)