[英]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.