2015-03-25 82 views
0

我是android的絕對新手。 我有一個位圖,我從URL中獲取。我想在位於屏幕頂部的圖像視圖中間顯示它。我也想要一個圖像邊框。目前,當我用下面的代碼轉動它只是去頂:如何在Android中設置邊框和旋轉位圖圖像?

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

當我設置這樣的形象是不是在市中心,又如何設置邊框呢?

我期待着這樣的事情。

enter image description here

任何可能的方式來實現,這是明顯的。請在這件事上給予我幫助。!

在此先感謝.. !!

回答

1
<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 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平面佈置旋轉創建一個XML文件。但是當它運行的時候,它會像這樣展示。

enter image description here

+0

問題是這個圖像中沒有邊框。 – 2015-04-03 12:42:35

+0

@Kannan_SJD請再次檢查我的答案。它也涵蓋了你的解決方案。 – Yogendra 2015-04-06 06:06:40

+0

謝謝.. !!我在檢查它 – 2015-04-06 06:43:05

0

如果您將填充= 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> 
+0

感謝您的幫助Jignesh .. !! – 2015-03-25 06:24:11

+0

但在這裏你已經給了src,我不打算用setImageBitmap設置圖像,那好嗎? – 2015-03-25 06:27:19

+0

現在檢查我的代碼,我已更新您的要求。 – 2015-03-25 06:43:04

1

使用此:

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

給這個代碼的ImageView

而且邊境請把ImageView的中的LinearLayout

,並給這個代碼的ImageView標籤內

android:padding="5dp" 

,如果你想邊框顏色則只是這樣做

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

感謝您的幫助Avishek ..! – 2015-03-25 06:23:49

1

Matrix mymatrix = new Matrix();

imageView.setScaleType(ScaleType.MATRIX);

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

imageView.setImageMatrix(mymatrix);

給予顏色到背景的LinearLayout

這將旋轉您的圖像視圖

set padding = 1px並將背景顏色設置爲linearlayout,那麼您將有1px邊框。 如果你想進一步澄清通知我

+0

感謝您的幫助Mesh .. !! – 2015-03-25 06:23:56