[原创]android学习之动画效果
1.布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度"
android:onClick="alpha"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"
android:onClick="scale"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
android:onClick="rotate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移"
android:onClick="translate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="综合"
android:onClick="all"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ImageView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/mm"
/>
</LinearLayout>
</LinearLayout>
2.activity程序的实现:
package com.example.animation_image_test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv = (ImageView) this.findViewById(R.id.iv);
}
public void alpha(View v)//透明度
{
AlphaAnimation an = new AlphaAnimation(0, 1);
an.setDuration(3000);
iv.startAnimation(an);
}
public void scale(View v)//缩放
{
ScaleAnimation an = new ScaleAnimation(0, 2,2,1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
an.setDuration(3000);
iv.startAnimation(an);
}
public void rotate(View v)//旋转
{
RotateAnimation an=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
an.setDuration(3000);
iv.startAnimation(an);
}
public void translate(View v)//平移
{
TranslateAnimation an=new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f);
an.setDuration(3000);
iv.startAnimation(an);
}
public void all(View v)//综合
{
AlphaAnimation an=new AlphaAnimation(0, 1);
an.setDuration(3000);
ScaleAnimation sc=new ScaleAnimation(0, 3, 1, 1,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sc.setDuration(3000);
RotateAnimation ra=new RotateAnimation(0, 270, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(3000);
TranslateAnimation ta=new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f);
ta.setDuration(3000);
AnimationSet animation=new AnimationSet(true);
//将效果加入到AnimationSet里面
animation.addAnimation(an);
animation.addAnimation(sc);
animation.addAnimation(ra);
animation.addAnimation(ta);
iv.startAnimation(animation);//执行动画效果
}
}
{:7_139:}支持我家玄玄~ :lol::lol: 楼主是自学android么 强烈支持楼主ing.... 玄玄厉害,高! 厉害!!! 一直想做移动端的开发,虽然大家都选择了安卓,但是我依然追寻WP:loveliness: 我只是路过打酱油的。 qingchen 发表于 2014-11-25 09:57
楼主是自学android么
呵呵! java基础与C是自学的,android的话是培训老师教的!{:5_109:} 秋之帆 发表于 2014-11-25 10:46
玄玄厉害,高!
呵呵!过奖了! 我疯狂我成功 发表于 2014-11-25 12:35
一直想做移动端的开发,虽然大家都选择了安卓,但是我依然追寻WP
哦! 那好啊!WP也好着呢!现在比较主流的就是iphone,android,wp了 青玄 发表于 2014-11-25 15:25
呵呵! java基础与C是自学的,android的话是培训老师教的!
我也在学android但是还在学基础 青玄 发表于 2014-11-25 15:25
呵呵! java基础与C是自学的,android的话是培训老师教的!
<p><div class="blockcode"><blockquote><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ImgActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp">
<Button
android:id="@+id/img_tigaotumingdu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activity_img_tigaotoumingdu"
android:textSize="16sp" />
<Button
android:id="@+id/img_jiangditoumingdu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activity_img_jingditoumingdu"
android:textSize="16sp" />
<Button
android:id="@+id/img_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activity_img_nextbtn"
android:textSize="16sp"/>
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@drawable/bj1"
android:scaleType="fitCenter"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp">
<Button
android:id="@+id/img_dengbisuofang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activity_img_dengbisuofang"
android:textSize="16sp"/>
<Button
android:id="@+id/img_caiqiesuofang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activity_img_caiqiesuofang"
android:textSize="16sp"/>
<Button
android:id="@+id/img_bianxingsuofang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activity_img_bianxingsuofang"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
package com.ljc.cn;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
public class ImgActivity extends Activity {
private Button imgNext;
private ImageView imgView;
private Button imgPlus;
private Button imgMinus;
private Button imgFitCenter;
private Button imgCenterCrop;
private Button imgFitXY;
private static int alpha = 180;
private static int i = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_img);
imgNext = (Button)findViewById(R.id.img_next);
imgView = (ImageView)findViewById(R.id.imageView1);
imgPlus = (Button)findViewById(R.id.img_tigaotumingdu);
imgMinus = (Button)findViewById(R.id.img_jiangditoumingdu);
imgFitCenter = (Button)findViewById(R.id.img_dengbisuofang);
imgCenterCrop = (Button)findViewById(R.id.img_caiqiesuofang);
imgFitXY = (Button)findViewById(R.id.img_bianxingsuofang);
//方法一:为提高透明度和降低透明度都添加一个监听器
// imgPlus.setOnClickListener(new OnClickListener() {
// public void onClick(View arg0) {
// alpha += 20;
// if (alpha >= 255) {
// Toast.makeText(ImgActivity.this, "透明度不能再提高了!", Toast.LENGTH_SHORT).show();
// }
// }
// });
// imgMinus.setOnClickListener(new OnClickListener() {
// public void onClick(View arg0) {
// alpha -= 20;
// if (alpha <= 0) {
// Toast.makeText(ImgActivity.this, "透明度不能再降低了!", Toast.LENGTH_SHORT).show();
// }
// }
// });
// 方法二:为为提高透明度和降低透明度添加一个通用监听器
OnClickListener listener1 = new OnClickListener() {
public void onClick(View arg0) {
if (arg0 == imgPlus) {
alpha += 20;
}
if (arg0 == imgMinus) {
alpha -= 20;
}
if (alpha >=255) {
alpha = 255;
Toast.makeText(ImgActivity.this, "透明度不能再提高了!", Toast.LENGTH_SHORT).show();
}
if (alpha <= 0) {
alpha = 0;
Toast.makeText(ImgActivity.this, "透明度不能再降低了!", Toast.LENGTH_SHORT).show();
}
imgView.setAlpha(alpha);
}
};
imgPlus.setOnClickListener(listener1);
imgMinus.setOnClickListener(listener1);
//下一张按钮事件的实现
final int picId[] = {R.drawable.bj1,R.drawable.bj2,R.drawable.bj3,R.drawable.img2,R.drawable.img3};
imgNext.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int num = picId.length;
if (i < num) {
imgView.setImageResource(picId);
i++;
if (i>=num) {
i = 0;
}
}
}
});
//方法一:为每一个按钮添加监听器
// imgFitCenter.setOnClickListener(new OnClickListener() {
// public void onClick(View arg0) {
// imgView.setScaleType(ScaleType.FIT_CENTER);
// }
// });
// imgCenterCrop.setOnClickListener(new OnClickListener() {
// public void onClick(View arg0) {
// imgView.setScaleType(ScaleType.CENTER_CROP);
// }
// });
// imgFitXY.setOnClickListener(new OnClickListener() {
// public void onClick(View arg0) {
// imgView.setScaleType(ScaleType.FIT_XY);
// }
// });
//方法二:为三个按钮添加同一个监听器
OnClickListener listener = new OnClickListener() {
public void onClick(View arg0) {
switch (arg0.getId()) { //获取单击按钮的id选择相对应的事件
case R.id.img_dengbisuofang:
imgView.setScaleType(ScaleType.FIT_CENTER);
break;
case R.id.img_caiqiesuofang:
imgView.setScaleType(ScaleType.CENTER_CROP);
break;
case R.id.img_bianxingsuofang:
imgView.setScaleType(ScaleType.FIT_XY);
break;
default:
imgView.setScaleType(ScaleType.FIT_CENTER);
break;
}
}
};
//为三个按钮设置新建的监听器
imgFitCenter.setOnClickListener(listener);
imgCenterCrop.setOnClickListener(listener);
imgFitXY.setOnClickListener(listener);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.img, menu);
return true;
}
}
{:5_109:}
qingchen 发表于 2014-11-26 16:14
package com.ljc.cn;
import android.os.Bundle;
不错啊! 恩恩!多谢分享!以后还要多多交流呢!{:5_109:} qingchen 发表于 2014-11-26 16:14
package com.ljc.cn;
import android.os.Bundle;
不错啊! 恩恩!多谢分享!以后还要多多交流呢!{:5_109:}
页:
[1]