我是编程和java的新手,我正在开发一个使用eclipse的Android壁纸应用程序,我不能添加很多照片,因为主线程无法处理所有这些东西所以我厌倦了一个新的线程所以我可以放照片,但我不能不知道我应该在run方法中写什么。所以,如果你能告诉我我应该写什么以及要改变什么,如果有可能的话会有一个很好的例子,谢谢。
P.S:请不要建议使用AsyncTask,因为我不知道如何使用它。
这是我的代码:
public class SET2 extends Activity implements OnClickListener {
ImageView display;
int toPhone2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.set2);
toPhone2 = R.drawable.iv1;
display = (ImageView) findViewById(R.id.ImageV1);
ImageView image1 = (ImageView) findViewById(R.id.IView1);
ImageView image2 = (ImageView) findViewById(R.id.IView2);
ImageView image3 = (ImageView) findViewById(R.id.IView3);
ImageView image4 = (ImageView) findViewById(R.id.IView4);
ImageView image5 = (ImageView) findViewById(R.id.IView5);
ImageView image6 = (ImageView) findViewById(R.id.IView6);
Button setwall = (Button) findViewById(R.id.button5);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
setwall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.IView1:
display.setImageResource(R.drawable.iv1);
toPhone2 = R.drawable.iv1;
break;
case R.id.IView2:
display.setImageResource(R.drawable.iv2);
toPhone2 = R.drawable.iv2;
break;
case R.id.IView3:
display.setImageResource(R.drawable.iv3);
toPhone2 = R.drawable.iv3;
break;
case R.id.IView4:
display.setImageResource(R.drawable.iv4);
toPhone2 = R.drawable.iv4;
break;
case R.id.IView5:
display.setImageResource(R.drawable.iv5);
toPhone2 = R.drawable.iv5;
break;
case R.id.IView6:
display.setImageResource(R.drawable.iv6);
toPhone2 = R.drawable.iv6;
break;
case R.id.button5:
Toast addEggs = Toast.makeText(MainActivity.this, "Wallpaper Set", Toast.LENGTH_SHORT);
addEggs.show();
InputStream WP = getResources().openRawResource(toPhone);
Bitmap wallpaper = BitmapFactory.decodeStream(WP);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), toPhone);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try{
wallpaperManager.setBitmap(bitmap);
getApplicationContext().setWallpaper(wallpaper);
}catch(IOException e){
e.printStackTrace();
}
break;
}
}
答案 0 :(得分:0)
首先,避免使用巨大的切换语句。
其次,最好的方法是使用AsyncTask
但是,您可以通过简单快捷的方式制作新主题:
...
case R.id.button5:
Toast addEggs = Toast.makeText(MainActivity.this, "Wallpaper Set", Toast.LENGTH_SHORT);
addEggs.show();
Thread t = new Thread(new Runnable(){
public void run(){
final Bitmap bmWallpaper = loadImage(id);
runOnUiThread(new Runnable(){
public void run() {
setWallpaperBitmap(bmWallpaper);
}
});
});
t.start();
break;
}
}
private Bitmap loadImage(int id) {
InputStream WP = getResources().openRawResource(id);
Bitmap wallpaper = BitmapFactory.decodeStream(WP);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), toPhone);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
return bitmap;
}
private void setWallpaperBitmap(Bitmap bitmap) {
try{
wallpaperManager.setBitmap(bitmap);
getApplicationContext().setWallpaper(wallpaper);
}catch(IOException e){
e.printStackTrace();
//Never do this! Handle your exceptions
}
}