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了。
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了。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件