如何旋转 ImageView? [英] How can I rotate an ImageView?

查看:31
本文介绍了如何旋转 ImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的活动中以编程方式旋转 ImageView.目前我找不到任何适用于 Xamarin 的解决方案,并且来自 Android 的翻译解决方案也不起作用.有人知道如何旋转 ImageView 吗?

I want to rotate an ImageView programmatically in my Activity. Currently I couldn't find any solutions for Xamarin which works and translated solutions from Android didn't work either. Does anybody know how to rotate an ImageView?

例如这段代码只是给了我一个 NullPoinerException 而我不知道为什么:

This code for example just gives me a NullPoinerException and I don't know why:

[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

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>

推荐答案

Bitmap bMap = BitmapFactory.DecodeResource(Resources, Resource.Id.imageView1);

您的应用将失败的地方.为什么?您传入的资源 ID 不是用于实际图像,而是用于 ImageView 实例.DecodeResource 用于解码图像而不是视图.

Is where your app will fail. Why? The Resource Id you are passing in is not for an actual image, but for the ImageView instance. DecodeResource is used for decoding images and not views.

相反,您应该执行以下操作:

Instead you should do something like:

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

这篇关于如何旋转 ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆