开发者

"Set wallpaper" button function, Android

开发者 https://www.devze.com 2023-01-10 22:54 出处:网络
I\'ve created a custom gallery; however, the \"Set wallpaper\" button will not set the wallpaper. Here\'s the wallpaper.java I have. I\'m just lost on how to implement OnClickListener and then set my

I've created a custom gallery; however, the "Set wallpaper" button will not set the wallpaper. Here's the wallpaper.java I have. I'm just lost on how to implement OnClickListener and then set my button to use an onclicklistener like so:

buttonName.setOnClickListener(this);

package com.totem;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup; 
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView; 
import android.widget.Gallery.LayoutParams;

import java.io.IOException;
import java.io.InputStream;

public class Wallpaper extends Activity implements
    AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {

private static final String LOG_TAG = "Home";

private static final Integer[] THUMB_IDS = {
    R.drawable.andy_small,

};

private static final Integer[] IMAGE_IDS = {
       R.drawable.andy,

};

private Gallery mGallery;
private boolean mIsWallpaperSet;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.wallpaper_chooser);

    mGallery = (Gallery) findViewById(R.id.gallery);
    mGallery.setAdapter(new ImageAdapter(this));
    mGallery.setOnItemSelectedListener(this);
    mGallery.setOnItemClickListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    mIsWallpaperSet = false;
}

@SuppressWarnings("unchecked")
    public void onItemSelected(AdapterView parent, View v, int position, long id) {
    getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
}

@SuppressWarnings("unchecked")
    public void onItemClick(AdapterView parent, View v, int position, long id) {
    selectWallpaper(position);
}

private synchronized void selectWallpaper(int position) {
    if (mIsWallpaperSet) {
        return;
    }
    mIsWallpaperSet = true;
    try {
        InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
        setWallpaper(stream);
        setResult(RESULT_OK);
        finish();
    } catch (IOException e) {
        Log.e(LOG_TAG, "Failed to set wallpaper " + e);
    }
}

@SuppressWarnings("unchecked")
    public void onNothingSelected(AdapterView parent) {
}

public boolean onTouchEvent(MotionEvent event, Object Action_Down) {
    getAction(Action_Down);
    selectWallpaper(mGallery.getSelectedItemPosition());
    return true;
}

    public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(THUMB_IDS[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
开发者_JAVA百科                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        i.setBackgroundResource(android.R.drawable.picture_frame);
        return i;
    }

}

} Parsed in 0.242 seconds, using GeSHi 1.0.8.4


Go to http://www.xtensivearts.com/topics/tutorials/ there are video tutorials that will help you with the onclicklisteners


are you set user permission in androidmanifest file for set Wallpaper?

    <uses-permission android:name="android.permission.SET_WALLPAPER" />
0

精彩评论

暂无评论...
验证码 换一张
取 消