Andorid Studio 制作欢乐写数字(Timer 启动 + 帧动画)
if(i < 25){ //当前图片小于 25
String name = "on1_" + i;
//获取图片资源 id
int imgid = getResources().getIdentifier(name,"drawable","com.example.msi.writenumber");
iv_frame.setBackgroundResource(imgid);
i++;
}
if(j == 24){ //如果当前图片的位置为 24
if(typedialog){ //没有对话框的情况下
dialog(); //调用书写完成对话框方法
}
}
}
实现代码如下:
其中 R.drawable.frame1 为放置所有帧的 anmianimation-list
duration 为帧持续的时间为 0.15s 即 150ms
public void OnYS(View v){
if(mdiaolg == null){
mdiaolg = new mCustomProgressDialog(this,"演示中单击边缘取消",R.drawable.frame1);
}
mdiaolg.show();
}
//继承 dialog 代码如下:
public class mCustomProgressDialog extends ProgressDialog{
private AnimationDrawable mAnimation; //设置对话框的动画资源
private Context mContext;
private ImageView mImageView;
private String mLoadingTip; //设置对话框文字
private TextView mLoadingTv; //显示对话框文字
private int mResid; //资源 id
public mCustomProgressDialog(Context context,String content,int id) {
super(context);
this.mContext = context;
this.mLoadingTip = content;
this.mResid = id;
//设置单击对话框周边是否让 dialog 消失,设置为 True
setCanceledOnTouchOutside(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_dialog);
//获取布局文件中的 TextView
mLoadingTv = findViewById(R.id.loadingTv);
mImageView = findViewById(R.id.loadingIv);
if(mResid == 0){
mImageView.setBackgroundDrawable(null);
}else {
mImageView.setBackgroundResource(mResid);
}
mAnimation = (AnimationDrawable) mImageView.getBackground();
//为了防止在 onCreate 方法中只显示第一帧的解决方案之一
mImageView.post(new Runnable() {
@Override
public void run() {
mAnimation.start();
}
});
mLoadingTv.setText(mLoadingTip);
}
}
还有操作时候根据鼠标滑动来实现图片切换效果
根据实时滑动屏幕的 x2,y2 来判断手势位置,画完之后弹出 dialog 完成或者再来一次
如果在中途手离开屏幕,这是耗时操作(?),则发送 message,子线程接收 message 进行 handle 将图片慢慢的撤回到第一帧
实现代码如下:
linearLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){ //获取行动方式头部
case MotionEvent.ACTION_DOWN: //手指按下事件
x1 = event.getX(); //获取手指按下的 x 坐标
y1 = event.getY(); //获取按下的 y 坐标
igvx = iv_frame.getLeft(); //获取手指按下图片的 x 坐标
igvy = iv_frame.getTop(); //图片的 y 坐标
//判断当手指按下的坐标大于图片位置的坐标时,证明手指按住移动,开始书写
if(x1 >= igvx && x1 <= igvx + (int)(arrdown.getWidth() * scaleWidth)
&& y1 >= igvy && y1 <= igvy + (int)(arrdown.getWidth() * scaleWidth) ){
type = 1; //开启书写
}else {
type = 0; //否则关闭书写
}
break;
case MotionEvent.ACTION_MOVE: //手势移动中判断
igvx = iv_frame.getLeft();
igvy = iv_frame.getTop();
x2 = event.getX();
y2 = event.getY();
//下面是根据笔画以及手势做图片的处理,滑到不同位置,加载不同图片
if(type == 1){
if(x2 >= igvx && x2 <= igvx + (int)(arrdown.getWidth() * scaleWidth)){
if(y2 <= igvy +(int)(arrdown.getHeight() * scaleHeight) / 24 &&
y2 >= igvy)
lodimagep(1);
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 2){
lodimagep(2);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 3){
lodimagep(3);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 4){
lodimagep(4);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 5){
lodimagep(5);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 6){
lodimagep(6);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 7){
lodimagep(7);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 8){
lodimagep(8);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 9){
lodimagep(9);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 10){
lodimagep(10);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 11){
lodimagep(11);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 12){
lodimagep(12);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 13){
lodimagep(13);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 14){
lodimagep(14);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 15){
lodimagep(15);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 16){
lodimagep(16);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 17){
lodimagep(17);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 18){
lodimagep(18);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 19){
lodimagep(19);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 20){
lodimagep(20);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 21){
lodimagep(21);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 22){
lodimagep(22);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 23){
lodimagep(23);
}
else if(y2 <= igvy + (int)(arrdown.getHeight() * scaleHeight) / 24 * 24){
lodimagep(24);
}
}
}
break;
case MotionEvent.ACTION_UP: //手势抬起判断
type = 0;
if(touchTimer != null){ //中断计时器
touchTimer.cancel();
touchTimer = null;
}
touchTimer = new Timer();
touchTimer.schedule(new TimerTask() {
@Override
public void run() {
Thread thread = new Thread(new Runnable() { //创建子线程
@Override
public void run() {
//创建 Message 用于发送消息
Message message = new Message();
message.what = 2; //Message 消息为 2
//发送消息给 handle 实现倒退显示图片
mHandler.sendMessage(message);
}
});
thread.start();
}
},300,200);
}
return true;
}
});
}
评论