【问题标题】:How to rotate images in GridView如何在 GridView 中旋转图像
【发布时间】:2018-04-10 22:56:55
【问题描述】:

使用带有 Mvvm Cross 的 Xamarin Android 在 GridView 中显示了一组图像,但问题是有两个按钮可旋转时钟和反时钟方向,每次单击时所有单元格(或图像)都应旋转 90 度网格视图。 我怎样才能实现它? 这是我的 viewModel 集合,它将绑定到 GridView,

private ObservableCollection<Snapshot> _snapshotsItemSource;
    public ObservableCollection<Snapshot> SnapshotsItemSource
    {
        get { return _snapshotsItemSource; }
        set
        {
            _snapshotsItemSource = value;
            RaisePropertyChanged(() => SnapshotsItemSource);
        }

    }

我的模型对象是,

public class Snapshot : MvxViewModel
{
    string _ID;
    public string ID
    {
        get
        {
            return _ID;
        }
        set
        {
            _ID = value;
            ImageUrl = string.Format("http://{0}:{1}/GetSnapshot?ID={2}", TIConstants.Device.IpAdd, TIConstants.PortNo, value);
        }
    }

    string _ImageUrl;
    public string ImageUrl
    {
        get
        {
            return _ImageUrl;
        }
        set
        {
            _ImageUrl = value;
            RaisePropertyChanged("ImageUrl");
        }
    }
}

我的观点是,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mvvm="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<MvvmCross.Binding.Droid.Views.MvxGridView
    android:id="@+id/gvSnapshots"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numColumns="3"
    android:layout_alignParentTop="true"
    mvvm:MvxItemTemplate="@layout/snapshotcellview"
    mvvm:MvxBind="ItemsSource SnapshotsItemSource" />
<RelativeLayout
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="@dimen/section_height"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <ImageButton
        android:id="@+id/btnRotateLeft"
        android:layout_alignParentLeft="true"
        android:src="@drawable/abc_text_select_handle_left_mtrl_dark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageButton
        android:id="@+id/btnRotateRight"
        android:layout_alignParentRight="true"
        android:src="@drawable/abc_text_select_handle_left_mtrl_dark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

【问题讨论】:

    标签: xamarin gridview xamarin.android image-rotation


    【解决方案1】:

    试试 Glide,对你有帮助吗

    Glide
        .with( context )
        .load( "image Url" )
        .transform( new RotateTransformation( context, 90f ))
        .into( imageView );
    

    【讨论】:

    • 我想为 Gridview 添加它,而不是为单个图像视图添加,Glide 无法为我的项目添加另一件事,显示 Glide.xamarin 无法通过 Nuget 包添加
    【解决方案2】:
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
    
            base.OnCreateView(inflater, container, savedInstanceState);
            var view = this.BindingInflate(Resource.Layout.SnapshotsView, null);
    
            var toolbar = ((MainActivity)this.Activity).FindViewById(Resource.Id.toolbar);
            var txtTitle = ((MainActivity)this.Activity).FindViewById<TextView>(Resource.Id.TitleText);
            txtTitle.Text = ViewModel.ViewTitle;
            RegisterEvents();
            ViewModel.SetItemSource();
    
            var btnLeft = view.FindViewById<ImageButton>(Resource.Id.btnRotateLeft);
            btnLeft.Click += BtnLeft_Click;
    
            var btnRight = view.FindViewById<ImageButton>(Resource.Id.btnRotateRight);
            btnRight.Click += BtnRight_Click;
    
            grid = view.FindViewById<MvxGridView>(Resource.Id.gvSnapshots);
            mAdapter = new SnapshotsDataViewAdapter(this.Activity, (IMvxAndroidBindingContext)BindingContext);
            grid.Adapter = mAdapter;
            grid.OnItemClickListener = this;
            return view;
        }
    
        void BtnLeft_Click(object sender, EventArgs e)
        {
            currentRotation = currentRotation + 1;
            mAdapter.NotifyDataSetChanged();
        }
    
        void BtnRight_Click(object sender, EventArgs e)
        {
            currentRotation = currentRotation - 1;
            mAdapter.NotifyDataSetChanged();
        }
    
    static int currentRotation = 0;
        static float RotationTranslation = 0;
        private class SnapshotsDataViewAdapter : MvxAdapter
        {
            SnapshotsViewModel mViewModel;
            public SnapshotsDataViewAdapter(FragmentActivity context, IMvxAndroidBindingContext bindingContext) : base(context, bindingContext)
            {
                mViewModel = bindingContext.DataContext as SnapshotsViewModel;
            }
    
            protected override View GetBindableView(View convertView, object dataContext, ViewGroup parent, int templateId)
            {
                View row = convertView;  
    
                try 
                {  
                    var item = (BSSnapshot)dataContext;
                    if (row == null) 
                    {  
                       row = BindingContext.BindingInflate(templateId, parent, false);  
                    }  
    
    
                    var imgView = row.FindViewById<ImageView>(Resource.Id.imgRotate1);  
                    Picasso.With(Android.App.Application.Context).Load(item.ImageUrl).Into(imgView);
    
                    imgView.Rotation = currentRotation*90;
    
                }
                catch (Exception ex) 
                {  
                    System.Diagnostics.Debug.WriteLine(ex.Message);  
                } 
                finally 
                {}  
                return row; 
            }
    
        }
    

    在这里我使用了 Picasso,因为 Glide 无法添加到项目中并且它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-03
      • 2017-04-14
      • 2016-02-04
      • 2015-04-04
      • 2011-06-25
      • 2016-07-20
      • 2021-11-30
      • 2011-07-20
      相关资源
      最近更新 更多