2019年1月26日 星期六

[Android]_ch11.12_圖片輪播元件



[Android]_ch11.12_圖片輪播元件

package com.derek.ch33_animationdrawable;



import android.app.Activity;

import android.content.res.Resources;

import android.graphics.drawable.AnimationDrawable;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.MotionEvent;

import android.widget.ImageView;

import android.widget.TextView;





public class MainActivity extends Activity {

ImageView iv;

TextView tv;

Drawable s1,s2,s3,s4,s5,s6;

AnimationDrawable animationDrawable;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        iv=(ImageView)findViewById(R.id.imageView1);

        tv=(TextView)findViewById(R.id.textView1);

        Resources resources=getResources();

        s1=resources.getDrawable(R.drawable.s1);

        s2=resources.getDrawable(R.drawable.s2);

        s3=resources.getDrawable(R.drawable.s3);

        s4=resources.getDrawable(R.drawable.s4);

        s5=resources.getDrawable(R.drawable.s5);

        s6=resources.getDrawable(R.drawable.s6);

        animationDrawable=new AnimationDrawable();

        animationDrawable.addFrame(s1, 200);

        animationDrawable.addFrame(s2, 100);

        animationDrawable.addFrame(s3, 100);

        animationDrawable.addFrame(s4, 400);

        animationDrawable.addFrame(s5, 100);

        animationDrawable.addFrame(s6, 100);

        animationDrawable.setOneShot(false);

        iv.setBackground(animationDrawable);

        animationDrawable.start();

    }

@Override

public boolean onTouchEvent(MotionEvent event) {

if(event.getAction()==MotionEvent.ACTION_DOWN){

if(!animationDrawable.isRunning())

animationDrawable.start();

else{

animationDrawable.stop();

Drawable drawable=animationDrawable.getCurrent();

if(drawable.equals(s1))

tv.setText("點數 1");

else if(drawable.equals(s2))

tv.setText("點數 2");

else if(drawable.equals(s3))

tv.setText("點數 3");

else if(drawable.equals(s4))

tv.setText("點數 4");

else if(drawable.equals(s5))

tv.setText("點數 5");

else if(drawable.equals(s6))

tv.setText("點數 6");

}

}

return super.onTouchEvent(event);

}

   

}


沒有留言:

張貼留言

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...