2015-03-25 151 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