0
該代碼包含一個按鈕(必須添加到清單文件中)的圖庫,我只需要一個關於按鈕操作的建議;放在裏面有什麼「imageIDs *或位引用其先前被點擊,顯示的圖像我如何製作一個按鈕作爲設置壁紙按鈕?
下面是完整的代碼:
package net.keivan.gallery;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
//---the images to display---
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic8,
R.drawable.pic9,
R.drawable.pic10,
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
//---Displays the name of images as just as i click on them---
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
//---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
//---Set Wallpaper button---
public void onClick(View v) {
try {
/*at this part can you suggest what to put inside "imageIDs* or *bitmap* to reference
to the image which previously is clicked and shown*/
/* first.the activity is created second.the image which i clicked is shown third.whin
the user click on the button the image which previously is clicked on is set as background.*/
WallpaperManager.getInstance(this).setResource(imageIDs[position]);
//WallpaperManager.getInstance(this).setBitmap(mBitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
///////////////////////////////////////////////
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
{ getApplicationContext().getWallpaper(); }
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
謝謝非常非常有用和完整 – user1952003
還有關於圖片的另一個問題;正如你所提到的有兩種類型來設置backgruo nd 1.byimageIDs 2.通過mBitmaps.so我需要你的建議,如果你願意,我怎麼能把**的** **或**圖像的名稱,我以前在第一部分中選擇。提前Thanx – user1952003
隨着你分享的代碼,我不能說超過:WallpaperManager.getInstance(this).setResource(imageIDs [position]); –