vistaでandroid studio その16

# 概要
vistaでandroid studio 1.0.1やってみた。
練習問題やってみた。

# 練習問題
この画像を使って、走る人を表示せよ。

![sprite-animation4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/e93b05e1-6516-1de3-9cce-d781128ba335.png)

# 方針
- アセットを使う。
- ハンドラーを使う。
- カスタムビュー作る。
- drawbitmap使う。

# 参考にしたページ

https://qiita.com/shunXnegi/items/566540696ec1623d2b45

# 写真

![device-2022-09-05-055410.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/3f5c7e22-70a4-d742-5bb1-840f808c841f.png)


# サンプルコード

```
package com.ohisamallc.ohiapp154.ohiapp154;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.net.Uri;
import android.opengl.EGL14;
import android.opengl.GLES10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends ActionBarActivity {
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private CustomView mView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = new CustomView(this);
        mView.init();
        setContentView(mView);
    }
    class CustomView extends View {
        Bitmap mSprite;
        int mSizeX;
        int mSize;
        int mPixWidth;
        int mPixHeight;
        int mWillRepeat;
        int mCurrentFrame;
        int mRepeated;
        Rect mCurrentRect;
        Rect mDestRect;
        boolean mIsStarted;
        Runnable mRunnable;
        int repeatTimes;
        int interval;
        public CustomView(Context context) {
            super(context);
        }
        public CustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
        public void init() {
            interval = 100;
            repeatTimes = 10;
            int delay = 100;
            AssetManager assetManager = getAssets();
            InputStream istr = null;
            try
            {
                istr = assetManager.open("sprite-animation4.png");
                Log.e("ohi", "ok0");
            }
            catch (IOException e)
            {
                Log.e("ohi", "err0");
            }
            mSprite = BitmapFactory.decodeStream(istr);
            mCurrentFrame = 0;
            mRepeated = 0;
            mCurrentRect = new Rect();
            mDestRect = new Rect();
            mIsStarted = true;
            mPixWidth = 85;
            mPixHeight = 102;
            mSizeX = 6;
            final Handler handler = new Handler();
            final Runnable r = new Runnable() {
                @Override
                public void run() {
                    if (repeatTimes != 0 && mRepeated >= repeatTimes)
                    {
                        handler.removeCallbacks(this);
                    }
                    else
                    {
                        invalidate();
                        handler.postDelayed(this, interval);
                    }
                }
            };
            handler.post(r);
        }
        @Override
        protected void onDraw(final Canvas canvas) {
            super.onDraw(canvas);
            if (!mIsStarted)
            {
                return;
            }
            final int col = mCurrentFrame % mSizeX;
            final int row = mCurrentFrame / mSizeX;
            mCurrentRect.set(col * mPixWidth, row * mPixHeight, (col + 1) * mPixWidth, (row + 1) * mPixHeight);
            mDestRect.set(0, 0, getWidth(), getHeight());
            canvas.drawBitmap(mSprite, mCurrentRect, mDestRect, null);
            mCurrentFrame++;
            if (mCurrentFrame >= 30)
            {
                mCurrentFrame = 0;
            }
        }
    }
}

```

以上。