图像不裁剪 [英] Image not cropping

查看:123
本文介绍了图像不裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置一个背景图片的LinearLayout 是这样的:

I am setting a background image to LinearLayout like this:

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> 

2。 tile.xml

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/pie_chart_background"
        android:tileMode="repeat">
    </bitmap>

我现在设置在 back.xml 作为一个背景的LinearLayout 它工作得很好。

now I am setting the back.xml as a background to LinearLayout it works fine.

我需要有一个图片圆角,也是一个边框以它。但是我只有带有圆角的是在我的code中的问题的边界,而不是像我这么想吗?

I need to have a image with rounded corners and also a border to it.But i only have the border with rounded corners and not the image what is the problem in my code am i missing something?

这是怎么了我的形象的样子:

this is how my image looks:

推荐答案

花费数小时的时间您的问题背后,终于我实现了,希望现在只要你想,请通过以下code,它会给你同样的结果让我知道它的工作与否?

After spending hours of time behind your issue, finally I achieved, hope now it will give you same result as you want, please go through below code and let me know whether its works or not?

传递适当的参数以下函数来获取圆角与你的愿望颜色的边框。

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;
    }

的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>

的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));

    }

原图

输出

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

Below links help me to achieve my goal:

<一个href="http://stackoverflow.com/questions/11012556/border-over-a-bitmap-with-rounded-corners-in-android/12543803#12543803">Border在带圆角的位图在Android中

创建的ImageView带圆角

<一个href="http://stackoverflow.com/questions/2459916/how-to-make-an-imageview-to-have-rounded-corners">How作出的ImageView有圆角

如何设置paint.setColor(INT颜色)

这篇关于图像不裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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