android設置圖片變化的四種效果代碼


activity代碼如下:

package com.example.chapter12_graphic_animation;

 

import android.os.Bundle;

import android.app.Activity;

import android.content.res.Resources;

import android.graphics.drawable.Drawable;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

import android.view.animation.RotateAnimation;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

import android.widget.Button;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

 

private Button b1,b2,b3,b4;

 

private ImageView girlImage;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Resources res = getResources();

        Drawable drawable = res.getDrawable(R.drawable.test);

        this.getWindow().setBackgroundDrawable(drawable);

        setContentView(R.layout.main);

        

        girlImage=(ImageView)findViewById(R.id.ImageView01);

        

        b1=(Button)findViewById(R.id.Button01);

        b2=(Button)findViewById(R.id.Button02);

        b3=(Button)findViewById(R.id.Button03);

        b4=(Button)findViewById(R.id.Button04);

        

        

        

        b1.setOnClickListener(new OnClickListener(){

 

public void onClick(View arg0) {

//創建Scale尺寸變化動畫

Animation scaleAnimation =new ScaleAnimation(0f,1f,0f,1f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0.5f);

//設置動畫持續的時常

scaleAnimation.setDuration(3000);

//開始動畫

girlImage.startAnimation(scaleAnimation);

 

 

}

        

        });

        

        b2.setOnClickListener(new OnClickListener(){

 

public void onClick(View arg0) {

//創建Scale尺寸變化動畫

Animation alphaAnimation =new AlphaAnimation(0.1f,1.0f);

//設置動畫持續的時常

alphaAnimation.setDuration(3000);

 

//開始動畫

girlImage.startAnimation(alphaAnimation);

 

 

}

        

        });

        b3.setOnClickListener(new OnClickListener(){

 

public void onClick(View arg0) {

//創建Scale尺寸變化動畫

Animation translateAnimation =new TranslateAnimation(10,100,10,100);

//設置動畫持續的時常

translateAnimation.setDuration(3000);

//開始動畫

girlImage.startAnimation(translateAnimation);

 

 

}

        

        });

        b4.setOnClickListener(new OnClickListener(){

 

public void onClick(View arg0) {

//創建Scale尺寸變化動畫

Animation rotateAnimation =new RotateAnimation(0f,+360f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0.5f);

//設置動畫持續的時常

rotateAnimation.setDuration(3000);

//開始動畫

girlImage.startAnimation(rotateAnimation);

 

 

}

        

        });

    }

    

    //為按鈕添加監聽事件

    

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

}

 

XML代碼設置如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

 

    <Button

        android:id="@+id/Button01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Test Scale.." />

 

    <Button

        android:id="@+id/Button02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        

        android:text="Test Alpha..." />

 

    <Button

        android:id="@+id/Button03"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

       

        android:text="Test Translate..." />

 

    <Button

        android:id="@+id/Button04"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

       

        android:text="Test Rotate..." />

 

    <ImageView

        android:id="@+id/ImageView01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:src="@drawable/girl" />

 

</LinearLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM