如何更改在屏幕上绘制的图片

时间:2011-12-30 05:18:45

标签: android android-image

这可能看起来像是一个愚蠢的问题,但你如何改变画在屏幕上的图片。我已经能够编写一个应用程序,如果它绘制一个小图标,你触摸屏幕。所以我自然完成后我想通过添加一个选项菜单以及更改你被淹的图标的能力来改善它,但是当我运行代码时,图标图片保持不变。当我看着它时,我发现当你点击任何一个菜单项它确实做了它的工作并更改了图像ID但是当你回到主屏幕并尝试创建一个新图像时它又恢复到旧图像。我不知道为什么它不会改变因为当我看在它一切都有意义,它可以正确地改变图标。如果任何人对我做错了什么或任何关于如何做到这一点的建议有任何想法,那将非常感激

主要

  public class main extends Activity {  

 /** Called when the activity is first created. */  



  MenuItem item2;  

  int item3=R.drawable.ic_launcher;  



 @Override  

 public void onCreate(Bundle savedInstanceState) {  

     super.onCreate(savedInstanceState);  

    FrameLayout sv = new FrameLayout(this);  

     LinearLayout ll = new LinearLayout(this);  

     Panel test = new Panel(this);  

     //ImageButton button = new ImageButton(this);  



     ll.setOrientation(LinearLayout.VERTICAL);  

     sv.addView(test);  

     //ll.addView(button);  

     sv.addView(ll);  



     setContentView(sv);  



 }  



 public boolean onCreateOptionsMenu(Menu menu) {  

     // TODO Auto-generated method stub  

     MenuInflater inflater = getMenuInflater();  

     inflater.inflate(R.menu.menu, menu);  

     return super.onCreateOptionsMenu(menu);  

 }  



 @Override  

 public boolean onOptionsItemSelected(MenuItem item) {  

     // TODO Auto-generated method stub  



     Log.v("test", "item3 before is: "+item3);  



       item3=R.drawable.box;  



       Log.v("test", "item3 after is: "+item3);  





     return super.onOptionsItemSelected(item);  

     }  
  } 

小组

 public class Panel extends SurfaceView implements SurfaceHolder.Callback {  

 private Bitmap image;  

 private ViewThread mThread;  

 private int x;  

 private int y;  

 private ArrayList<Element> mElements = new ArrayList<Element>();  



 public Panel(Context context) {  

     super(context );  

     image = BitmapFactory.decodeResource(getResources(),yantz.imageapp4.R.drawable.test);  

     getHolder().addCallback(this);  

     mThread = new ViewThread(this);  



 }  



 public void doDraw(Canvas canvas) {  

     canvas.drawColor(Color.CYAN);  
     canvas.drawBitmap(image, x, y, null);  

     synchronized (mElements){  

     for(Element element : mElements){  

         element.doDraw(canvas);  

     }  

     }  





 }  









 @Override  

 public boolean onTouchEvent(MotionEvent event) {  

     // TODO Auto-generated method stub  

     Log.v("test", "you have touched the sreen: ");  



     synchronized (mElements){  

     mElements.add(new Element(getResources(),(int) event.getX(),(int) event.getY()));  

 }  

 return super.onTouchEvent(event);  

 }  







 @Override  

 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  

     // TODO Auto-generated method stub  

 }  



 @Override  

 public void surfaceCreated(SurfaceHolder holder) {  

     if (!mThread.isAlive()) {  

         mThread = new ViewThread(this);  

         mThread.setRunning(true);  

         mThread.start();  

     }  

 }  



 @Override  

 public void surfaceDestroyed(SurfaceHolder holder) {  

     if (mThread.isAlive()) {  

         mThread.setRunning(false);  

     }  

 }  

 }

元素

 public class Element extends main{  

 private int mX;  

 private int mY;  

 int location ;  

 private Bitmap mBitmap;  



 public Element(Resources res, int x, int y) {  



     Log.v("element", "item3 before location is: "+item3);  



     location =item3;  

     mBitmap = BitmapFactory.decodeResource(res, location);  

     mX = x - mBitmap.getWidth() / 2;  

     mY = y - mBitmap.getHeight() / 2;  

     Log.v("element", "item3 before location is: "+item3);  



 }  







 public void doDraw(Canvas canvas) {  

     canvas.drawBitmap(mBitmap, mX, mY, null);  

 }  



 public void setlocation(int location2){  

     location=location2;  

 }  

} 

ViewThread

    public class ViewThread extends Thread {  

     private Panel mPanel;  

     private SurfaceHolder mHolder;  

     private boolean mRun = false;  



     public ViewThread(Panel panel) {  

         mPanel = panel;  

         mHolder = mPanel.getHolder();  

     }  



     public void setRunning(boolean run) {  

         mRun = run;  

     }  



     @Override  

     public void run() {  

         Canvas canvas = null;  

         while (mRun) {  

             canvas = mHolder.lockCanvas();  

             if (canvas != null) {  

                 mPanel.doDraw(canvas);  

                 mHolder.unlockCanvasAndPost(canvas);  

             }  

         }  

     }  

}

1 个答案:

答案 0 :(得分:0)

你可以使用

@Override
protected void onResume() {
    super.onResume();
    id="what ever you want";
//and set it to imagevIew;
}

如果我正确理解了这个问题,就会发生这种情况,因为当您的活动没有聚焦时,您的活动会暂停,并以默认值恢复。