bufferInterface类:java代码示例
/*
* 这是一个缓冲界面类
* init()函数用来初始化界面;
* initImage()函数用来初始化Imageview数组;
* playAniamtion()函数用来创建一个进程,循环打印;
* cleanImage()函数用来消除Imageview数组里面的内容;
*/
public class bufferInterface {
private static Animation anm; //首先先定义一个动画对象
public static int marginsTop;
public bufferInterface() {
// TODO Auto-generated constructor stub
}
public static Animation init(Context context,LinearLayout layout)
{
DisplayMetrics dm=context.getResources().getDisplayMetrics();
//dm获取手机屏幕参数
int height=dm.heightPixels;
bufferInterface.marginsTop=height-200;
Log.i("marginsTop2", String.valueOf(marginsTop));
anm=AnimationUtils.loadAnimation(context,R.anim.myanim);
//加载动画的类型
layout.setBackgroundResource(R.drawable.background);
return anm;
}
public static void playAniamtion(final Handler handler)
{
new Thread()
{
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int runcount=0;
for(int a=1;a<=4;a++)
{
if(runcount<2)
{
for(int i=0;i<=6;i++)
{
handler.sendEmptyMessage(i);
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
runcount++;
}else
{
handler.sendEmptyMessage(99);
runcount=0;
}
}
}
}.start();
}
public static void initImage(Context context,LinearLayout layout,List<ImageView> images)
{
layout.setGravity(Gravity.CENTER_HORIZONTAL);//设置Linearlayout的居中类型
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(110, 110);
//设置单个imageview的布局大小
param.setMargins(30, marginsTop, 0, 0);
//设置单个imageview在父视图上的位置
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(110, 110);
param2.setMargins(-5,marginsTop, 0, 0);
ImageView l = new ImageView(context);
l.setLayoutParams(param);
layout.addView(l);
images.add(l);
ImageView o = new ImageView(context);
o.setLayoutParams(param2);
layout.addView(o);
images.add(o);
ImageView a = new ImageView(context);
a.setLayoutParams(param2);
layout.addView(a);
images.add(a);
ImageView d = new ImageView(context);
d.setLayoutParams(param2);
layout.addView(d);
images.add(d);
ImageView i = new ImageView(context);
i.setLayoutParams(param2);
layout.addView(i);
images.add(i);
ImageView n = new ImageView(context);
n.setLayoutParams(param2);
layout.addView(n);
images.add(n);
ImageView g = new ImageView(context);
g.setLayoutParams(param2);
layout.addView(g);
images.add(g);
}
public static void clearImage(List<ImageView> images)
{
for(ImageView image:images)
{
image.setImageDrawable(null);
image.destroyDrawingCache(); //销毁imageview中的缓存
}
}
}
上面是我个人在前辈的基础上,写的一个bufferInterface类,意思就是缓冲界面啦
R.anim.myanim.xml代码,此代码用来设置动画播放的方式
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.5" //开始x轴的相对于原图的倍数关系
android:toXScale="1.2" //结束x轴的相对于原图的倍数关系
android:fromYScale="0.5"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:startOffset="-50"
android:duration="100"
/>
</set>
主函数调用的java代码
public class mainActivity extends Activity {
public LinearLayout layout;
private Animation anm;
public List<ImageView> images;
public Handler handler=new Handler()
{
public void handleMessage(android.os.Message msg) {
switch(msg.what)
{
case 0:
images.get(0).setImageDrawable(mainActivity.this.getResources().getDrawable(R.drawable.l));
images.get(0).startAnimation(anm);
break;
case 1:
images.get(1).setImageDrawable(mainActivity.this.getResources().getDrawable(R.drawable.o));
images.get(1).startAnimation(anm);
break;
case 2:
images.get(2).setImageDrawable(mainActivity.this.getResources().getDrawable(R.drawable.a));
images.get(2).startAnimation(anm);
break;
case 3:
images.get(3).setImageDrawable(mainActivity.this.getResources().getDrawable(R.drawable.d));
images.get(3).startAnimation(anm);
break;
case 4:
images.get(4).setImageDrawable(mainActivity.this.getResources().getDrawable(R.drawable.i));
images.get(4).startAnimation(anm);
break;
case 5:
images.get(5).setImageDrawable(mainActivity.this.getResources().getDrawable(R.drawable.n));
images.get(5).setAnimation(anm);
break;
case 6:
images.get(6).setImageDrawable(mainActivity.this.getResources().getDrawable(R.drawable.g));
images.get(6).setAnimation(anm);
break;
case 99:
Intent intent=new Intent(mainActivity.this, secondActivity.class);
startActivity(intent);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
requestWindowFeature(Window.FEATURE_NO_TITLE);
layout=new LinearLayout(this);
images=new ArrayList<ImageView>();
anm=UI.bufferInterface.init(this, layout);
UI.bufferInterface.initImage(this, layout, images);
UI.bufferInterface.playAniamtion(handler);
setContentView(layout);
}
}