Android手机循环切换图片全屏播放

在尝试使用TimerTask在Android中切换全屏图片时遇到程序崩溃问题。为解决此问题,参考大神的方法,改用Message进行图片的循环切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己写的手机全屏切换播放图片,开发的是android4.3,运行测试的手机是Android5.1的,布局文件就不说了,就是一个ImageView,代码如下:



import java.util.Timer;
import java.util.TimerTask;


import com.example.five.MainActivity.MyClickListener;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;


public class testpicture extends Activity {
Timer timer =null;
TimerTask timertask = null;
ImageView imageview = null;
static int count = 0;
int[] imageid = new int[] // 切换的图片资源id
{
R.drawable.picture2bmp,
R.drawable.picture1bmp,

};
int SIGN = 17;
int SIGNC = 16;
Bitmap sour = null;
private  Handler  handle = null;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);    //设置取消标题栏,一定要在setContentView之前,否则不能执行
        setContentView(R.layout.picture_test);
        imageview = (ImageView)findViewById(R.id.imageView1);
        sour = BitmapFactory.decodeResource(getResources(), R.drawable.picture1bmp);     //获取位图资源
        sour = pictureMatchScreen(sour);
        imageview.setImageBitmap(sour);
        setTimer();
        
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,             //设置取消状态栏
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
       
        handle = new Handler()                                      //接收消息
        {
        @Override
        public void handleMessage(Message msg)
        {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        if (msg.what == SIGN) 
        {
        sour = BitmapFactory.decodeResource(getResources(), imageid[count%3]);
              sour = pictureMatchScreen(sour);
              imageview.setImageBitmap(sour);
       // imageview.setImageResource(imageid[count%2]);
       
        }
        if(msg.what== SIGNC)
        {
         
        }
         }
       
        };
    }

public void setTimer()
{
timer = new Timer();
timertask = new TimerTask()
{
public void run()                     //时钟内部发送消息
{
count++;
Message msg = new Message();
msg.what = SIGN;
handle.sendMessage(msg);
}
};
timer.schedule(timertask, 2000, 1000);        //定义时钟每1s进一次run
}
/***
 * 自定义将图片缩放到和屏幕一样大
 * 
 */
public Bitmap pictureMatchScreen(Bitmap source)       
{
Display wm = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
wm.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
float width = (float)screenWidth/source.getWidth();
float height = (float)screenHeight/source.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(width, height);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

}

本来想在TimerTask的run里面直接切换图片的,程序直接崩溃,表示·····,送样没办法在网上看了大神的,只好用message了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值