如何获取当前鳍状肢视图的图像ID并将该图像设置为用户背景?

时间:2014-01-10 12:26:00

标签: android viewflipper

此代码工作正常,但当我尝试将当前图像设置为用户手机背景时发生错误。无法弄清楚如何从屏幕上的当前图像设置资源。如何在这里设置这一行

myWallpaperManager.setResource(vf.indexOfChild(vf.getCurrentView()));   

使用当前图片的视图ID?

这是错误: android.content.res.Resources $ NotFoundException:无法找到资源ID#0x2 这是我的代码。

   public class MainActivity extends Activity implements OnGestureListener, OnClickListener {

protected GestureDetector gestureScanner;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private Button next,previous,set;
private ViewFlipper vf;
private Animation animFlipInNext,animFlipOutNext, animFlipInPrevious, animFlipOutPrevious;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gestureScanner = new GestureDetector(this);

    //vf for viewflipper
    vf=(ViewFlipper)findViewById(R.id.ViewFlipper01);
    animFlipInNext = AnimationUtils.loadAnimation(this, R.anim.flipinnext);
    animFlipOutNext = AnimationUtils.loadAnimation(this, R.anim.flipoutnext);
    animFlipInPrevious = AnimationUtils.loadAnimation(this, R.anim.flipinprevious);
    animFlipOutPrevious = AnimationUtils.loadAnimation(this, R.anim.flipoutprevious);

    next = (Button) findViewById(R.id.Button01);
    previous = (Button) findViewById(R.id.Button02);
    set = (Button) findViewById(R.id.set);
    next.setOnClickListener(this);
    previous.setOnClickListener(this);
    set.setOnClickListener(this);
}

//@Override
public void onClick(View v) {
    if (v == next) {
        vf.setInAnimation(animFlipInNext);
        vf.setOutAnimation(animFlipOutNext);
        vf.showNext();
    }
    if (v == previous) {
        vf.setInAnimation(animFlipInPrevious);
        vf.setOutAnimation(animFlipOutPrevious);
        vf.showPrevious();
    }
    if(v == set) {
        WallpaperManager myWallpaperManager
        = WallpaperManager.getInstance(getApplicationContext());
       try {
           myWallpaperManager.setResource(R.drawable.arnab); // how to set this line to use the resources of current image on screen?

       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       Toast.makeText(getBaseContext(), "Wallpaper is successfully changed", Toast.LENGTH_SHORT).show();
    }
}
//this is the part to handle Gesture Listener
@Override
public boolean onTouchEvent(MotionEvent me){
    return gestureScanner.onTouchEvent(me);
}
public boolean onDown(MotionEvent e){
    return true;
}
//FLING gesture listener
public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
    try {
        if(e1.getX() > e2.getX() && Math.abs(e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(this.getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
            vf.setInAnimation(animFlipInPrevious);
            vf.setOutAnimation(animFlipOutPrevious);
            vf.showPrevious();
        }else if (e1.getX() < e2.getX() && e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(this.getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();
            vf.setInAnimation(animFlipInNext);
            vf.setOutAnimation(animFlipOutNext);
            vf.showNext();
        }
    } catch (Exception e) {
        // nothing
    }
    return true;
}
public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY){
    return true;
 }

2 个答案:

答案 0 :(得分:0)

此代码:

vf.indexOfChild(vf.getCurrentView());   

告诉您ViewFlipper的哪个页面。如果鳍状肢只包含图像(不嵌套在另一个布局中),那么您可能需要做的是:

myWallpaperManager.setDrawable( ((ImageView) vf.getCurrentView()).getDrawable() );   

解释第50行&amp;您只需linked here执行的代码中的51:

[Line 50] vf=(ViewFlipper)findViewById(R.id.viewFlipper01)
[Line 51] vf.setDisplayedChild(vf.indexOfChild(findViewById(R.id.viewFlipper01)));

第51行说的是“将我的子视图设置为我的子视图的索引,实际上是我自己”。如果该行在英语中没有意义,那么在Java中,不能将自己包含在子视图中时,它就更没意义了

答案 1 :(得分:0)

bitmap =((BitmapDrawable)((ImageView)vf.getCurrentView())。getDrawable())。getBitmap(); myWallpaperManager.setBitmap(位图);

这是我的代码!

`