Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image not cropping

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>

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

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:

enter image description here

like image 803
Goofy Avatar asked Jan 23 '13 09:01

Goofy


People also ask

How do I crop a picture without it zooming in?

Press-and-hold the Shift key, grab a corner point, and drag inward to resize the selection area. Because you're holding the Shift key as you scale, the aspect ratio (the same ratio as your original photo) remains exactly the same.

Why can't I crop my image in Illustrator?

Because Adobe Illustrator is a vector-based design system, you can't crop a picture in Illustrator the same way you can in Adobe photoshop. But you can use a clipping mask and opacity mask to crop a picture and photo.

Why can't I crop my image in Photoshop?

Crop Command Unavailable (Grayed Out) Fortunately, this problem is also extremely simple to fix. In order to use the Crop command located in the Image menu, you need to have an active selection in your image. The Crop command doesn't offer any options, it simply crops your image to the boundaries of your selection.


2 Answers

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?

Pass appropriate parameter to below function to get rounded corner with your desire color border.

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

    }

Original image

enter image description here

Output

enter image description here

Below links help me to achieve my goal:

Border over a bitmap with rounded corners in Android

Creating ImageView with round corners

How to make an ImageView to have rounded corners

How to set paint.setColor(int color)

like image 58
RobinHood Avatar answered Sep 22 '22 11:09

RobinHood


try your back.xml some thing like like this instead of giving a rectangle background image.

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:drawable="#ff00ff >
    </item>
 <item>
    <shape android:shape="rectangle" >
        <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> 
like image 25
Raj Avatar answered Sep 22 '22 11:09

Raj