我的应用正在做的是在屏幕上显示图像,当用户点击"下一步"或"返回"按钮,它改变为另一张图片。它为他们提供了将图片设置为壁纸或下载图像的选项。我需要该应用程序才能检测到屏幕上的图像,以便下载正确的图像。这是我的代码:
package com.dogger20011.mcwallpapers;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Portrait extends Activity {
private ImageView hImageViewSemafor;
private Button hButton;
private Button hButton2;
FileOutputStream outStream;
int currentImage = 0;
int[] images = { R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.background1 };
int[] imagesb = { R.drawable.background1, R.drawable.b3, R.drawable.b2, R.drawable.b1 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.portrait);
hImageViewSemafor = (ImageView)findViewById(R.id.imageView1);
hButton = (Button) findViewById(R.id.button2);
hButton.setOnClickListener(aButtonChangeImageListener);
hImageViewSemafor = (ImageView)findViewById(R.id.imageView1);
hButton2 = (Button) findViewById(R.id.button1);
hButton2.setOnClickListener(bButtonChangeImageListener);
}
View.OnClickListener aButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
currentImage++;
currentImage = currentImage % images.length;
hImageViewSemafor.setImageResource(images[currentImage]);
}
};
View.OnClickListener bButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
currentImage++;
currentImage = currentImage % imagesb.length;
hImageViewSemafor.setImageResource(imagesb[currentImage]);
}
};
public void myClickHandler(View arg0){
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(CURRENT IMAGE);
} catch (IOException e) {
e.printStackTrace();
}
}
public void myClickHandler2(View v) throws IOException{
Bitmap bm = BitmapFactory.decodeResource( getResources(), CURRENT IMAGE);
File file = new File(Environment.getExternalStorageDirectory() + "/Download/Background1.jpg");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
}
}
答案 0 :(得分:2)
在ImageView
hImageViewSemafor上通过hImageViewSemafor.setImageResource()
设置图像资源时,只需将一些类成员变量设置为放在hImageViewSemafor中的资源的id。然后使用该变量的值来确定屏幕上的图像。
private int currentImageResId;
View.OnClickListener aButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
currentImage++;
currentImage = currentImage % images.length;
hImageViewSemafor.setImageResource(images[currentImage]);
currentImageResId = images[currentImage];
}
};
View.OnClickListener bButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
currentImage++;
currentImage = currentImage % imagesb.length;
hImageViewSemafor.setImageResource(imagesb[currentImage]);
currentImageResId = images[currentImage];
}
};
public void myClickHandler(View arg0){
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(currentImageResId);
} catch (IOException e) {
e.printStackTrace();
}
}
答案 1 :(得分:2)
因为没有获取资源id int ImageView
类,所以可以使用setTag(Object object)
来保存资源ID。
View.OnClickListener aButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
currentImage++;
currentImage = currentImage % images.length;
hImageViewSemafor.setImageResource(images[currentImage]);
hImageViewSemafor.setTag(images[currentImage]);
}
};
public void myClickHandler(View arg0){
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource((Integer)hImageViewSemafor.getTag());
} catch (IOException e) {
e.printStackTrace();
}
}