[英]Scale and rotate an image in android - image won't appear
I am new to android, and I am having problems making my image appear in my second activity after I use the same scale and rotate code with my first activity. 我是android的新手,在我使用相同的比例尺并在第一次活动中旋转代码后,在第二次活动中出现图像时出现了问题。 In first activity I am currently retrieving an image from the photo gallery. 在第一个活动中,我目前正在从相册中检索图像。 This image I then display in an ImageView. 然后,我将这个图像显示在ImageView中。 Before I actually display the image I have to scale the bitmap and then rotate it. 在实际显示图像之前,我必须先缩放位图,然后旋转它。 This is working - and I got most of the code from this site - thanks! 它正在工作-我从该站点获得了大部分代码-谢谢! I then save the details of this image to my SQLite database (as in the path name of image on sd card) However, in the second activity - I am then trying to retrieve the image immediately for display in an imageview (from it's pathname - which I have saved to SQLite database), and again I use the same code to scale and rotate the same image to display in this ImageView, however this time, the image does not appear.....? 然后,我将该图像的详细信息保存到我的SQLite数据库中(如sd卡上图像的路径名)。但是,在第二个活动中,我试图立即检索该图像以在imageview中显示(从其路径名-将其保存到SQLite数据库中),然后再次使用相同的代码缩放和旋转相同的图像以在此ImageView中显示,但是这次,该图像没有出现.....? If I just execute the code for scaling the bitmap - the image does appear, but it is only when I try to execute the rotate code that the image won't appear. 如果我只是执行用于缩放位图的代码-图像确实出现,但是只有当我尝试执行旋转代码时,图像才会出现。 As said - in first activity - the scale AND rotate works, but for second activity - only the scale works. 如前所述,在第一个活动中,比例尺和旋转有效,但在第二个活动中,仅比例尺有效。 Here is the code for scaling and rotating 这是缩放和旋转的代码
public static void process (String filePath, ImageView imageView) {
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
int h = 250;
int w = 250;
Bitmap scaled = Bitmap.createScaledBitmap(yourSelectedImage, h, w, true);
Matrix matrix = new Matrix();
imageView.setScaleType(ScaleType.MATRIX);
imageView.setImageBitmap(scaled);
matrix.postRotate(90f, imageView.getDrawable().getBounds().width()/2,
imageView.getDrawable().getBounds().height()/2);
imageView.setImageMatrix(matrix);
}
XML of first activity: 第一项活动的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/result"
android:layout_width="250dip"
android:layout_height="250dip"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:contentDescription="@string/desc1"
android:scaleType="matrix" />
<Button
android:id="@+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginBottom="15dp"
android:text="Select Photo" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/selectButton"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="27dp"
android:text="Save" />
<LinearLayout
android:id="@+id/addbloglinkcontainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/result" >
<TextView
android:id="@+id/addbloglinktext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_blog_link" />
<EditText
android:id="@+id/blogLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="10"
android:ems="10"
android:inputType="textUri" />
</LinearLayout>
</RelativeLayout>
XML of second activity: 第二项活动的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView"
android:layout_width="250dip"
android:layout_height="250dip"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:contentDescription="@string/desc1"
android:onClick="mthdtocall"
android:scaleType="matrix" />
</RelativeLayout
Dont know why the image doesnt appear but... You could try to use LRU cache to reuse the same bitmap image in both activities. 不知道为什么不显示图像,但是...您可以尝试在两个活动中使用LRU缓存重用同一位图图像。
developer.android.com/reference/android/util/LruCache.html developer.android.com/reference/android/util/LruCache.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.