此代碼工作正常,但是當我嘗試設置當前圖像到用戶手機背景我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;
}
我tryed該行它告訴我 方法 getDrawable() 是未定義類型查看 如何定義getDrawable? – user3181702
我已經更新了我的答案,但這不是一個非常安全的方法來處理這個。您可能希望將索引圖映射到資源ID。 –
now .setDrawable未定義:/ 如何製作該地圖? – user3181702