我想在我的Activity中以编程方式旋转ImageView。目前我无法找到适用于Xamarin的任何解决方案,Android的翻译解决方案也无法正常工作。有人知道如何旋转ImageView吗?
这个代码例如只给我一个NullPoinerException并且我不知道原因:
[Trace] error opening trace file: No such file or directory (2)
[Mono] Image addref mscorlib[0xb7c3b4b8] -> mscorlib.dll[0xb7c771b0]: 1
[Mono] AOT module 'mscorlib.dll.so' not found: Cannot load library: load_library[1093]: Library '/data/data/ImageViewRotation.ImageViewRotation/lib/mscorlib.dll.so' not found
[Mono] GC_MAJOR: (mature allocation failure) pause 1.41ms, total 1.34ms, bridge 0.02ms major 112K/32K los 0K/0K
[Mono] Assembly mscorlib[0xb7c3b4b8] added to domain RootDomain, ref_count=1
[Mono] Assembly Loader probing location: '/data/data/ImageViewRotation.ImageViewRotation/files/.__override__/ImageViewRotation.dll'.
[Mono] Image addref ImageViewRotation[0xb7c6aac0] -> /data/data/ImageViewRotation.ImageViewRotation/files/.__override__/ImageViewRotation.dll[0xb7c6a4a0]: 2
[Mono] Assembly ImageViewRotation[0xb7c6aac0] added to domain RootDomain, ref_count=1
[Mono] AOT module '/data/data/ImageViewRotation.ImageViewRotation/files/.__override__/ImageViewRotation.dll.so' not found: Cannot load library: load_library[1093]: Library '/data/data/ImageViewRotation.ImageViewRotation/lib//data/data/ImageViewRotation.ImageViewRotation/files/.__override__/ImageViewRotation.dll.so' not found
[Mono] Assembly Loader loaded assembly from location: '/data/data/ImageViewRotation.ImageViewRotation/files/.__override__/ImageViewRotation.dll'.
[Mono] Config attempting to parse: '/data/data/ImageViewRotation.ImageViewRotation/files/.__override__/ImageViewRotation.dll.config'.
[Mono] Config attempting to parse: '/Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/a1e3982a/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/ImageViewRotation/ImageViewRotation.config'.
[monodroid-gc] GREF GC Threshold: 46800
[Mono] Image addref Mono.Android[0xb7c506d8] -> Mono.Android.dll[0xb7c6c678]: 1
[Mono] Assembly Mono.Android[0xb7c506d8] added to domain RootDomain, ref_count=1
[Mono] AOT module 'Mono.Android.dll.so' not found: Cannot load library: load_library[1093]: Library '/data/data/ImageViewRotation.ImageViewRotation/lib/Mono.Android.dll.so' not found
[Mono] The request to load the assembly mscorlib v2.0.5.0 was remapped to v2.0.0.0
[Mono] Unloading image mscorlib.dll [0xb7c85a78].
[Mono] Assembly Ref addref Mono.Android[0xb7c506d8] -> mscorlib[0xb7c3b4b8]: 2
[MonoDroid] Xamarin/Android Trial Mode Active
[dalvikvm-heap] Grow heap (frag case) to 10.964MB for 441772-byte allocation
[dalvikvm-heap] Grow heap (frag case) to 12.649MB for 1767052-byte allocation
[] HostConnection::get() New Host Connection established 0xb7c9c840, tid 2126
[Mono] The request to load the assembly System.Core v2.0.5.0 was remapped to v3.5.0.0
[Mono] Image addref System.Core[0xb7cc0320] -> System.Core.dll[0xb7cbf7c8]: 1
[Mono] Assembly System.Core[0xb7cc0320] added to domain RootDomain, ref_count=1
[Mono] AOT module 'System.Core.dll.so' not found: Cannot load library: load_library[1093]: Library '/data/data/ImageViewRotation.ImageViewRotation/lib/System.Core.dll.so' not found
[Mono] Assembly Ref addref Mono.Android[0xb7c506d8] -> System.Core[0xb7cc0320]: 2
[Mono] The request to load the assembly mscorlib v2.0.5.0 was remapped to v2.0.0.0
[Mono] Unloading image mscorlib.dll [0xb7cc2168].
[Mono] Assembly Ref addref System.Core[0xb7cc0320] -> mscorlib[0xb7c3b4b8]: 3
[Mono] Assembly Ref addref ImageViewRotation[0xb7c6aac0] -> Mono.Android[0xb7c506d8]: 2
[Mono] DllImport attempting to load: '__Internal'.
[Mono] DllImport loaded library '(null)'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_gc_wait_for_bridge_processing'.
[Mono] Probing '_monodroid_gc_wait_for_bridge_processing'.
[Mono] Found as '_monodroid_gc_wait_for_bridge_processing'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_get_identity_hash_code'.
[Mono] Probing '_monodroid_get_identity_hash_code'.
[Mono] Found as '_monodroid_get_identity_hash_code'.
[Mono] The request to load the assembly mscorlib v2.0.5.0 was remapped to v2.0.0.0
[Mono] Unloading image mscorlib.dll [0xb7d9b5f0].
[Mono] Assembly Ref addref ImageViewRotation[0xb7c6aac0] -> mscorlib[0xb7c3b4b8]: 4
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_gref_inc'.
[Mono] Probing '_monodroid_gref_inc'.
[Mono] Found as '_monodroid_gref_inc'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_gref_log_new'.
[Mono] Probing '_monodroid_gref_log_new'.
[Mono] Found as '_monodroid_gref_log_new'.
[MonoDroid] UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
[MonoDroid] at ImageViewRotation.MainActivity.OnCreate (Android.OS.Bundle) <0x000c2>
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x00048>
[MonoDroid] at (wrapper dynamic-method) object.f34fad60-56f9-4f99-879a-ff280dbcc703 (intptr,intptr,intptr) <0x0001f>
[mono]
[mono] Unhandled Exception:
[mono] System.NullReferenceException: Object reference not set to an instance of an object
[mono] at ImageViewRotation.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00000] in <filename unknown>:0
[mono] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00000] in <filename unknown>:0
[mono] at (wrapper dynamic-method) object:f34fad60-56f9-4f99-879a-ff280dbcc703 (intptr,intptr,intptr)
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
[mono-rt] at ImageViewRotation.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00000] in <filename unknown>:0
[mono-rt] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00000] in <filename unknown>:0
[mono-rt] at (wrapper dynamic-method) object:f34fad60-56f9-4f99-879a-ff280dbcc703 (intptr,intptr,intptr)
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Graphics;
namespace ImageViewRotation
{
[Activity (Label = "ImageViewRotation", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
ImageView image = FindViewById<ImageView> (Resource.Id.imageView1);
Matrix mat = new Matrix();
Bitmap bMap = BitmapFactory.DecodeResource(Resources, Resource.Id.imageView1);
mat.PostRotate(45);
Bitmap bMapRotate = Bitmap.CreateBitmap(bMap, 0, 0, bMap.Width, bMap.Height, mat, true);
image.SetImageBitmap(bMapRotate);
}
}
}
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@drawable/Icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView1" />
</LinearLayout>
答案 0 :(得分:2)
该行
Bitmap bMap = BitmapFactory.DecodeResource(Resources, Resource.Id.imageView1);
您的应用程序将失败的位置。为什么?您传入的资源ID不是用于实际图像,而是用于ImageView
实例。 DecodeResource
用于解码图像,而不是视图。
相反,你应该做类似的事情:
var drawable = image.Drawable;
using (var bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Config.ARGB_8888))
using(var canvas = new Canvas(bitmap))
{
drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
drawable.Draw(canvas);
var mat = new Matrix();
mat.PostRotate(45);
using(var bMapRotate = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mat, true))
image.SetImageBitmap(bMapRotate);
}