最近在QQ空间中看到了图片的自动下落宇上升,感觉挺好玩,如果把图片防止为心形或者其他类型,都可以进行自动的上升与下降,比如心形图片或着气泡,都会产生动态的页面
下面就是具体的操作:
先进行写布局文件.xml,下面的是布局文件,设置七个图标其id为
@+id/IV_heart01",
@+id/IV_heart02",
@+id/IV_heart03",
@+id/IV_heart04",
@+id/IV_heart05",
@+id/IV_heart06",
@+id/IV_heart07",
位置随意,大小随意
下面是布局文件的具体代码,只贴出图标部分代码
<ImageView
android:id="@+id/IV_heart02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/IV_biaoqing02"
android:layout_alignTop="@+id/textView_inform"
android:onClick="Heart_plus"
android:alpha="0.7"
android:src="@drawable/heart" />
<ImageView
android:id="@+id/IV_heart01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView_inform"
android:layout_marginRight="18dp"
android:layout_toLeftOf="@+id/IV_heart02"
android:onClick="Heart_plus"
android:alpha="0.7"
android:src="@drawable/heart" />
<ImageView
android:id="@+id/IV_heart03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bn_ciphertexttoplaintext"
android:layout_alignTop="@+id/textView_inform"
android:alpha="0.7"
android:onClick="Heart_plus"
android:src="@drawable/heart" />
<ImageView
android:id="@+id/IV_heart07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView_inform"
android:layout_marginRight="18dp"
android:onClick="Heart_plus"
android:layout_toLeftOf="@+id/IV_heart03"
android:alpha="0.7"
android:src="@drawable/heart" />
<ImageView
android:id="@+id/IV_heart05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Heart_plus"
android:layout_alignBottom="@+id/textView_inform"
android:layout_toLeftOf="@+id/IV_heart01"
android:alpha="0.7"
android:src="@drawable/heart" />
<ImageView
android:id="@+id/IV_heart04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Heart_plus"
android:layout_alignLeft="@+id/textView_inform"
android:layout_alignTop="@+id/textView_inform"
android:alpha="0.7"
android:src="@drawable/heart" />
<ImageView
android:id="@+id/IV_heart_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/IV_biaoqing02"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:alpha="0.7"
android:onClick="Heart_plus"
android:src="@drawable/heart" />
<ImageView
android:id="@+id/IV_heart06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView_inform"
android:layout_toRightOf="@+id/IV_heart04"
android:alpha="0.7"
android:onClick="Heart_plus"
android:src="@drawable/heart" />
写完布局文件后,就可以到MainActivity中进行实现,在实现时,我们首先需要绑定布局中的图标,如下面代码所示
public class MainActivity_0_opt extends Activity {
private ImageView image01_heart,image02_heart,image03_heart,image04_heart
,image05_heart,image06_heart,image07_heart,heart_plus; //定义新变量,为下面的绑定工作做准备
private int heart_count=0;
private TextView tv_heartplus;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_opt_0);
/*
* 图片移动效果的实现
*/
image01_heart = (ImageView) findViewById(R.id.IV_heart01); //绑定布局中的图片文件
image02_heart = (ImageView) findViewById(R.id.IV_heart02);
image03_heart = (ImageView) findViewById(R.id.IV_heart03);
image04_heart = (ImageView) findViewById(R.id.IV_heart04);
image05_heart = (ImageView) findViewById(R.id.IV_heart05);
image06_heart = (ImageView) findViewById(R.id.IV_heart06);
image07_heart = (ImageView) findViewById(R.id.IV_heart07);
heart_plus = (ImageView) findViewById(R.id.IV_heart_plus);
tv_heartplus= (TextView) findViewById(R.id.tv_heartplus);
}
}
在实现时,我们当让希望下落或者上升时越随机越好,所以我们创建一个线程类,如下面代码中,我们创建HeartDrop()作为一个新的线程
public class HeartDrop extends Thread
{
public void run(ImageView iv)
{
try {
sleep((long) (5000*Math.random()));
long time =(long) (10000*Math.random());
if(time<3000||time>9000)
{
time=6000;
}
TranslateAnimation animation1 = new TranslateAnimation(0,-1,1900,-1);//设置方向,初始位置和末位的设置
animation1.setDuration(time);//设置动画持续时间
animation1.setRepeatCount(-1);//设置重复次数
iv.setAnimation(animation1);
/** 开始动画 */
animation1.startNow();
/** 结束动画 */
//animation.cancel();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
为了能够实现打开界面时就能够实现图片的下落,我们使用onResume实现,如下图所示代码
@Override
protected void onResume() {
//System.out.println("Onresume");
super.onPause();
HeartDrop heart_01=new HeartDrop();
HeartDrop heart_02=new HeartDrop();
heart_01.run(image01_heart);
heart_01.run(image02_heart);
heart_01.run(image03_heart);
heart_02.run(image04_heart);
heart_02.run(image05_heart);
heart_02.run(image06_heart);
heart_01.run(image07_heart);
heart_01.start();
heart_02.start();
}
最后的运行效果如下图所示:(由于填写的是从上到下的坐标,所以图像从下往上升)
好了,谢谢大家,上面就是我的动态图片实现的过程