您的位置:首页 > 移动开发 > Android开发

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

2015-11-12 19:16 447 查看
自己写的手机全屏切换播放图片,开发的是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了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 图片 代码