如何设置ImageView以显示PNG中的不同位置? (Android精灵一样的动画) [英] How to set ImageView to show different position in PNG? (Android sprite alike animations)

查看:165
本文介绍了如何设置ImageView以显示PNG中的不同位置? (Android精灵一样的动画)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下png:

I have the following png:

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

Each Icon is 100X100 px. All in all 800X100 px.

我有以下ImageView xml:

I have the following ImageView xml:

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

我想设置 CycleStageImage 以显示不同的图标(100,100)在计时器间隔1秒来回。

I would like to set CycleStageImage to be show different Icon (100,100) on Timer Interval of 1 Second back and forth.

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

I am having a problem generating a code that moves on Axis of this PNG. I have tried the followings from multiple links over SOF, but with no luck:

         //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

I have followed the Android tutorial: https://developer.android.com/guide/topics/resources/drawable-resource.html#Clip

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

but with no luck.. It would be much appreciated the help with timer as well with the png.

谢谢!

推荐答案

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

Finally I got it to work, following these steps: (Although I admit its a bit lame...)


  1. 我将上面的PNG分成8个(100x100)PNG,每个PNG使用在线工具 - > PNG Splitter

  2. 我使用在线工具创建了GIF - GIF制作工具 - > http://gifmaker.me/

  3. 我已保存已保存gif在 Assets 文件夹下并更改了其属性 - >构建操作到 AndroidAsset

  4. 我创建了 WebView 并将其放在我的XML屏幕中。它看起来像这样:

  1. I divided the above PNG into 8 (100x100) PNGs each using online tool -> PNG Splitter
  2. I created a GIF using online tool - GIF Maker -> http://gifmaker.me/
  3. I have placed the saved gif under "Assets" folder and changed its properties-> build action to "AndroidAsset"
  4. I have created WebView and placed it inside my XML screen. it looks like this:

            <WebView
            xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/MyWebView"
             android:layout_width="100dp"
             android:layout_height="100dp"/>


  • 在我的活动中,我生成的代码如下:

  • In my Activity I generated the code as follows:

    WebView myWebView = (WebView)findViewById(Resource.Id.MyWebView);
    myWebView.loadUrl("file:///android_asset/Gif.gif");
    


  • 它就像一个魅力!

    我希望它能成为任何想要在Android应用程序中运行GIF的人的助手。

    I hope it will be of assistant to anyone who desires to accomplish running GIF's inside android application.

    祝你好运。

    这篇关于如何设置ImageView以显示PNG中的不同位置? (Android精灵一样的动画)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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