[英]How to set border and rotate a bitmap image in Android?
我絕對是android的新手。 我有一個從URL提取的位圖。 我想在屏幕頂部的圖像視圖中間顯示它。 我還希望在圖像周圍有邊框。 目前,當我旋轉以下代碼時,它只是轉到頂部:
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);
}
當我這樣設置時,圖像不在中央,還如何設置其周圍的邊框?
我期待這樣的事情。
實現此目的的任何可能方式都是可觀的。 請在這件事上給予我幫助。!
提前致謝..!!
用這個:
android:rotation="20"// 20 is the angle
將此代碼提供給ImageView
為了邊框,請將ImageView放在LinearLayout中
並將此代碼提供給ImageView標記內
android:padding="5dp"
如果您想要邊框顏色,請執行此操作
通過這種方式為LinearLayout背景提供顏色
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);
這將旋轉您的圖像視圖
設置padding = 1px並將Background color設置為linearlayout,那么邊框將為1px。 如果您想進一步澄清,請通知我
<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>
在可繪制的shadow_gray.xml中創建一個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>
它不會在xml的圖形布局中顯示旋轉。 但是當它運行時它將顯示如下。
如果將padding = 5和背景色放在Imageview中,則將有5px的白色邊框,並將主布局用作“線性”布局,並在其上設置背景圖像以實現此目的。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.