简体   繁体   中英

How to set border and rotate a bitmap image in Android?

I am an absolute newbie to android. I have a bitmap that I fetch from an URL. I want to show it in the middle of an image view that is at the top of my screen. I also want a border around the image. Currently when I rotate with the following code it just go to the top:

private static void imageWithBorders(ImageView im, View image){
Matrix matrix = new Matrix();
im.setScaleType(ScaleType.MATRIX);   //required
matrix.setRotate((float) -20f);
im.setImageMatrix(matrix);
im.setImageBitmap(bmp);
}

When I set like this the image is not at the center, Also how to set border around it?

I am expecting something like this.

在此处输入图片说明

Any possible ways to achieve this is appreciable. Please help me with this.!

Thanks in advance..!!

Use this:

android:rotation="20"// 20 is the angle

give this code to ImageView

And to border please put the ImageView in LinearLayout

and give this code to within ImageView tag

android:padding="5dp"

and if you want border color then just do this

give a color to the LinearLayout background by this

android:background="#ffffff"//#ffffff is the color code

Matrix mymatrix = new Matrix();

imageView.setScaleType(ScaleType.MATRIX);

mymatrix.postRotate((float) angle, pivX, pivY);

imageView.setImageMatrix(mymatrix);

this will rotate your image view

set padding=1px and set Background color to linearlayout , THen you'll have a 1px border. If you want further clarifications inform me

<LinearLayout 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:background="#10171C"
android:gravity="center" >


    <ImageView
        android:id="@+id/sample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        android:rotation="10"
        android:padding="2dp"
        android:background="@drawable/shadow_gray"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

create a xml file in your drawable shadow_gray.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:radius="2dp"
        android:topRightRadius="0dp" />

    <stroke
        android:width="1dp"
        android:color="@android:color/white" />

</shape>

it will not showing rotation in graphic layout in xml. but when it will run then it will showing like this.

在此处输入图片说明

If you put the padding=5 and the background color in the Imageview, you'll have a 5px white border and take main layout as Linear layout and set background image on that to achive this.

android:rotation="20"// 20 is the angle



      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:background="@drawable/test"
            android:layout_height="fill_parent" >

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="#ffffff"
            android:padding="5dp" >


                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:Background="@drawable/test1"
                    tools:ignore="ContentDescription" />


        </LinearLayout>
       </LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM