2014-01-10 72 views
2

此代碼工作正常,但是當我嘗試設置當前圖像到用戶手機背景我geting錯誤。無法弄清楚如何從屏幕上的當前圖像設置資源。如何在此設置此行如何獲取當前鰭狀肢的圖像ID查看並將該圖像設置爲用戶背景?

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; 
} 

回答

0

此代碼:

vf.indexOfChild(vf.getCurrentView()); 

告訴你,你是在其上ViewFlipper的頁面。如果腳蹼僅包含圖像(不是嵌套在另一個佈局內),那麼你可能需要做的是沿着線:

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

爲了解釋什麼線50 & 51在你剛剛linked here做的代碼:

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

第51行說的是「將我的子視圖設置爲我的子視圖的索引,實際上是我自己」。如果該行沒有意義在英語中,它在Java使得更少的意義,其中一個觀點無法自身作爲子視圖

+0

我tryed該行它告訴我 方法 getDrawable() 是未定義類型查看 如何定義getDrawable? – user3181702

+0

我已經更新了我的答案,但這不是一個非常安全的方法來處理這個。您可能希望將索引圖映射到資源ID。 –

+0

now .setDrawable未定義:/ 如何製作該地圖? – user3181702

0

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

這是我的代碼!

`

相關問題