此代码工作正常,但是当我尝试设置当前图像到用户手机背景我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