Animation动画

public class MainActivity extends Activity implements OnClickListener
    private Button btnAlpha=null;
    private ImageView imageView=null;
    private Button btnXmlAlpha=null;
    private Button btnScale=null;
    private Button btnXmlScale=null;
    private Button btnTranslate=null;
    private Button btnRotate=null;
    private Button btnSet=null;
    private Button btnXmlSet=null;
    
    private static int GET_IMAGE_ID=100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView) findViewById(R.id.image_view);
        
        
        btnAlpha=(Button) findViewById(R.id.button_alpha);
        btnAlpha.setOnClickListener(this);

        btnXmlAlpha=(Button) findViewById(R.id.button_xml_alpha);
        btnXmlAlpha.setOnClickListener(this);

        btnScale=(Button) findViewById(R.id.btn_scale);
        btnScale.setOnClickListener(this);

        btnXmlScale=(Button) findViewById(R.id.button_xml_scale);
        btnXmlScale.setOnClickListener(this);

        btnTranslate=(Button) findViewById(R.id.btn_translate);
        btnTranslate.setOnClickListener(this);
        
        btnRotate=(Button) findViewById(R.id.btn_rotate);
        btnRotate.setOnClickListener(this);

        imageView.setOnClickListener(this);

        btnSet=(Button) findViewById(R.id.button_set);
        btnSet.setOnClickListener(this);

        btnXmlSet=(Button) findViewById(R.id.button_xml_set);
        btnXmlSet.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if(v==btnAlpha){
            //定义Alpha动画  
            Animation animationAlpha = new AlphaAnimation(0.1f, 1.0f);
            // 设置动画时间
            animationAlpha.setDuration(5000);
            // 开始播放动画
            imageView.startAnimation(animationAlpha);
        }else if(v==btnXmlAlpha){
            //从xml文件载入动画alpha效果
            Animation animation=AnimationUtils.loadAnimation(this, R.anim.alpha);
            imageView.startAnimation(animation);
        }else if(v==btnScale){
            //定义SaleAnimation
            Animation animation=new ScaleAnimation(0, 1, 1,1);
            //设置时间
            animation.setDuration(3000);
            imageView.startAnimation(animation);
        }else if(v==btnXmlScale){
            //从xml文件中载入scale动画效果
            Animation animation=AnimationUtils.loadAnimation(this, R.anim.scale);
            imageView.startAnimation(animation);
        }if(v==btnTranslate){
            //定义TranslateAnimation
            Animation animation=new TranslateAnimation(0,0,300,0);
            animation.setDuration(3000);
            imageView.startAnimation(animation);
        }else if(v==btnRotate){
            Animation animation=new RotateAnimation(0, 3600,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(5000);
            //指定变化速度为线性变化  即匀速
            animation.setInterpolator(new LinearInterpolator());
            imageView.startAnimation(animation);

        }else if(v==imageView){
            //选择一张本地图片
            Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.INTERNAL_CONTENT_URI);            
            startActivityForResult(intent, GET_IMAGE_ID);
        }else if(v==btnSet){
            AnimationSet animationSet=new AnimationSet(true);
            //新建一个alpha动画
            Animation alphaAnimation =new AlphaAnimation(0, 1);

            alphaAnimation.setDuration(3000);
            //新建一个rotate动画 
            Animation rotateAnimation=new RotateAnimation(0, 3600, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(3000);
            //设置alpha动画延迟时间
            alphaAnimation.setStartOffset(1000);
            //将两个动画添加到AnimationSet中
            animationSet.addAnimation(alphaAnimation);
            animationSet.addAnimation(rotateAnimation);
            //开始动画
            imageView.startAnimation(animationSet);
        }else if(v==btnXmlSet){
            //从xml文件中载入一个动画集
            Animation animation=AnimationUtils.loadAnimation(this, R.anim.set);
            //设置动画速度为匀速
            animation.setInterpolator(new LinearInterpolator());
            imageView.startAnimation(animation);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        menu.add(0, 0, 0, "试试");    
        menu.add(0,1,1,"就试试");        
        return super.onCreateOptionsMenu(menu);
    }
    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==GET_IMAGE_ID){
            if(resultCode==RESULT_OK){//从本地加载一张图片
                 String[] projection = { MediaStore.Images.Media.DATA };
                    Cursor cursor = managedQuery(data.getData(), projection, null, null, null);
                    startManagingCursor(cursor);
                    int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    String imagePath= cursor.getString(column_index);
                    
                    //Toast.makeText(getApplicationContext(), imagePath, 1).show();
                    
                    FileInputStream is=null;
                    try {
                        is = new FileInputStream(imagePath);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Bitmap bitMap=BitmapFactory.decodeStream(is);
                    imageView.setImageBitmap(bitMap);
                    
            }
        }
    
    
    }
    
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 0:
            Intent intent=new Intent(this,FrameActivity.class);
            startActivity(intent);
            break;

        default:
            break;
        }
        return true;
    }

}

 

package com.yjy.day12_11;


import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class FrameActivity extends Activity implements OnClickListener {
    private Button btnStart=null;
    private Button btnStart1=null;
    private Button btnStop=null;
    private ImageView imageView=null;
    private AnimationDrawable animationDrawable=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);
        btnStart=(Button) findViewById(R.id.button_start);
        btnStart1=(Button) findViewById(R.id.button_start1);
        btnStop=(Button) findViewById(R.id.button_stop);
        imageView=(ImageView) findViewById(R.id.imageView);
        btnStart1.setOnClickListener(this);
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);        
    }
    @Override
    public void onClick(View v) {
        if(v==btnStart){
            animationDrawable= new AnimationDrawable();
            for(int i=0;i<8;i++){
                int resId = getResources().getIdentifier("p"+(i+1), "drawable", "com.yjy.day12_11");
                animationDrawable.addFrame(getResources().getDrawable(resId), 200);
            }
            imageView.setBackgroundDrawable(animationDrawable);
            animationDrawable.setOneShot(false);
            animationDrawable.start();
            
        }else if(v==btnStart1){
            animationDrawable=(AnimationDrawable) getResources().getDrawable(R.drawable.animation_list);
            imageView.setBackgroundDrawable(animationDrawable);
            animationDrawable.start();
        }else if(v==btnStop){
            animationDrawable.stop();
        }
    }
}

 

posted on 2012-12-18 09:22  @与非  阅读(1028)  评论(0编辑  收藏  举报