如何设置ImageView以显示PNG中的不同位置? (Android精灵一样的动画)

时间:2016-11-07 14:50:18

标签: timer imageview xamarin.android android-bitmap css-sprites

我有以下png: enter image description here

每个图标为100X100像素。总而言之800X100像素。

我有以下ImageView xml:

                <ImageView
                android:id="@+id/CycleStageImage"
                android:layout_width="100dp"
                android:layout_height="100dp">

我想将 CycleStageImage 设置为在1秒的定时器间隔来回显示不同的Icon(100,100)。

我在生成在此PNG的Axis上移动的代码时遇到问题。 我已经通过SOF上的多个链接尝试了以下内容,但没有运气:

         //first try - not working
        //Resources res = mainActivity.ApplicationContext.Resources;
        //Bitmap bitmap = BitmapFactory.DecodeResource(res, Resource.Id.CycleImage);
        //BitmapDrawable bitmapDrawable = new BitmapDrawable(Resources.System, bitmap);
        //ClipDrawable clipDrawable = new ClipDrawable(bitmapDrawable, GravityFlags.Center, ClipDrawable.Horizontal);
        //clipDrawable.SetBounds(100, 100, 100, 100);
        //clipDrawable.SetLevel(100);
        //imageView.SetImageResource(Android.Resource.Color.Transparent);
        //imageView.SetImageDrawable(clipDrawable);


        //second try - shows only part of the left top corner
        //double TUNNING = 0.5; //0.5 cut in half
        //Bitmap srcBmp = BitmapFactory.DecodeResource(Resources.System, cycleStage);
        //Bitmap modBmp = Bitmap.CreateBitmap(
        //    srcBmp,
        //    0,
        //    srcBmp.Height, // TUNNING
        //    srcBmp.Height,
        //    srcBmp.Height
        //    );

        //third try - same as the second try.        
        //int START_X = 0;
        //int START_Y = 100;
        //int WIDTH_PX = 100;
        //int HEIGHT_PX = 100;
        //// Crop bitmap 
        //Bitmap newBitmap = Bitmap.CreateBitmap(SOURCE_BITMAP, START_X, START_Y, WIDTH_PX, HEIGHT_PX, null, false);

        //// Assign new bitmap to ImageView 
        //imageView.SetImageBitmap(newBitmap);

我已经关注了Android教程: https://developer.android.com/guide/topics/resources/drawable-resource.html#Clip

但没有运气.. 非常感谢计时器的帮助以及png。

谢谢!

1 个答案:

答案 0 :(得分:1)

最后我按照以下步骤开始工作: (虽然我承认它有点蹩脚......)

  1. 我将上述PNG分为8个(100x100)PNG,每个PNG使用在线工具 - &gt; PNG Splitter
  2. 我使用在线工具创建了GIF - GIF Maker - &gt; http://gifmaker.me/
  3. 我已将保存的gif放在&#34; 资产&#34;文件夹并更改其属性 - &gt;构建对&#34; AndroidAsset &#34;的行动
  4. 我创建了 WebView 并将其放在我的XML屏幕中。它看起来像这样:

                <WebView
                xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/MyWebView"
                 android:layout_width="100dp"
                 android:layout_height="100dp"/>
    
  5. 在我的活动中,我按如下方式生成了代码:

    WebView myWebView = (WebView)findViewById(Resource.Id.MyWebView);
    myWebView.loadUrl("file:///android_asset/Gif.gif");
    
  6. 它就像一个魅力!

    我希望任何想要在Android应用程序中运行GIF的人都会有助手。

    祝你好运。

相关问题