如何在AndroidS中单击按钮时将ImageSwitcher中的图像设置为墙纸

时间:2013-11-16 04:50:00

标签: android xml imageswitcher

任何人都可以告诉我如何在按钮点击时将ImageSwitcher中的图像设置为壁纸。
ImageSwitcher的代码工作正常。
但我很喜欢将图像设置为壁纸.. <请帮助我,因为我需要尽快完成这项工作

这是我的代码..

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_gallery);

    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);

    mSwitcher.setFactory(this);

    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,

            android.R.anim.fade_in));

    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));


    Gallery g = (Gallery) findViewById(R.id.gallery);

    g.setAdapter(new ImageAdapter(this));

    g.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

    mSwitcher.setImageResource(mImageIds[position]);
}

public void onNothingSelected(AdapterView<?> parent) {
}


public View makeView() {

    ImageView i = new ImageView(this);

    i.setBackgroundColor(0xFF000000);

    i.setScaleType(ImageView.ScaleType.FIT_CENTER);

    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

    return i;
}

private ImageSwitcher mSwitcher;

public class ImageAdapter extends BaseAdapter {

    public ImageAdapter(Context c) {

        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;

    }

    public long getItemId(int position) {

        return position;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView i = new ImageView(mContext);


        i.setImageResource(mThumbIds[position]);

        i.setAdjustViewBounds(true);

        i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        i.setBackgroundResource(R.drawable.apps_background);

        return i;
    }

    private Context mContext;

}

private Integer[] mThumbIds = {
        R.drawable.s1, R.drawable.s2,R.drawable.s,
        R.drawable.s3, R.drawable.s5,R.drawable.s6,
        R.drawable.s7, R.drawable.s9,R.drawable.s11,R.drawable.s12,R.drawable.snew,
        R.drawable.s13, R.drawable.sachin14,R.drawable.sachin15,R.drawable.sachin16};

private Integer[] mImageIds = {
        R.drawable.s1,R.drawable.s2,R.drawable.s,R.drawable.s3,
        R.drawable.s5,R.drawable.s6,R.drawable.s7,R.drawable.s9,R.drawable.s11, R.drawable.s12, R.drawable.snew,
        R.drawable.s13,R.drawable.s14,R.drawable.s15,R.drawable.s16};

}

Xml文件:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/apps_background" >

<ImageSwitcher android:id="@+id/switcher"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_alignParentTop="true"

    android:layout_alignParentLeft="true"

/>

<Gallery

    android:id="@+id/gallery"

    android:layout_width="match_parent"

    android:layout_height="60dp"

    android:layout_alignParentBottom="true"

    android:layout_alignParentLeft="true"

    android:layout_marginBottom="50dp"

    android:background="#55000000"

    android:gravity="center_vertical"

    android:spacing="16dp" />

<Button
    android:id="@+id/btnShare"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="set as wallpaper" />

 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

1:声明新变量 private int idselected = 0;

2:onItemSelected

idselected = position;

3:按钮单击 //壁纸管理器myWallpaperManager myWallpaperManager.setResource(mImageIds [idselected]);