2011-06-24 24 views
0

我已經得到了構成我的應用程序的以下類,它將魚眼失真放在位圖上。當我加載位圖時,它只在觸摸屏幕或滑動條時顯示,並且在視圖的構造函數完成後不顯示位圖。我已經把一些日誌記錄在那裏,基本上ondraw()被調用,但沒有畫到畫布上。我怎樣才能讓應用程序視圖構造函數完成後直接失效?如何強制invalidate()?

public class TouchView extends View{ 

    private File tempFile; 
    private byte[] imageArray; 
    private Bitmap bgr; 
    private Bitmap bm; 
    private Bitmap bgr2 = null;; 
    private Paint pTouch; 
    private float centreX = 1; 
    private float centreY = 1; 
    private int radius = 50; 
    private int Progress = 1; 
    private static final String TAG = "*********TouchView"; 
    private Filters f = null; 
    private boolean AsyncRunning = false; 
    // private MyTask mt = null; 



    public TouchView(Context context) { 
     super(context); 
     // TouchView(context, null); 
    } 




    public TouchView(Context context, AttributeSet attr) { 
     super(context,attr); 
     Log.e(TAG, "++++++++++ inside touchview constructor"); 



     tempFile = new File(Environment.getExternalStorageDirectory(). 
       getAbsolutePath() + "/"+"image.jpg"); 

     imageArray = new byte[(int)tempFile.length()]; 


    try{ 

      InputStream is = new FileInputStream(tempFile); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      DataInputStream dis = new DataInputStream(bis); 


      int i = 0; 

      while (dis.available() > 0) { 
      imageArray[i] = dis.readByte(); 
      i++; 
      } 

      dis.close(); 

     } catch (Exception e) { 

       e.printStackTrace(); 
      } 



     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
     bfo.inSampleSize = 1; 

     bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo); 


     bgr = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig()); 
     bgr = bm.copy(bm.getConfig(), true); 
     bgr2 = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig()); 



     f = new Filters(); 


     pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);   
     pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
     pTouch.setColor(Color.TRANSPARENT); 
     pTouch.setStyle(Paint.Style.STROKE); 

     Log.e(TAG, "++++++++++ end od touchview constructor"); 
    }// end of touchView constructor 


    public void findCirclePixels(){ 

     Log.e(TAG, "++++++++++ inside fpc"); 
       new Thread(new Runnable() { 
       public void run() { 
        float prog = (float)Progress/150000; 

       final Bitmap bgr3 = f.barrel(bgr,prog,centreX,centreY); 
        TouchView.this.post(new Runnable() { 
        public void run() { 
         TouchView.this.bgr2 = bgr3; 
         TouchView.this.invalidate(); 
        } 
        }); 
       } 
       }).start(); 


     /* 
     float prog = (float)Progress/150000; 
     long startTime = System.currentTimeMillis(); 
     bgr2 = f.barrel(bgr,prog); 
     long endTime = System.currentTimeMillis(); 
     long duration = endTime - startTime; 
     Log.e(TAG, "++++++++++ barrel() took "+ duration+" ms"); 
     */ 

       Log.e(TAG, "++++++++++ end fpc"); 
     }// end of changePixel() 




    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 

     switch (ev.getAction()) { 

      case MotionEvent.ACTION_DOWN: { 

       centreX = (int) ev.getX(); 
       centreY = (int) ev.getY(); 
       //findCirclePixels(); 
       TouchView.this.invalidate(); 

       break; 
      } 

      case MotionEvent.ACTION_MOVE: { 

        centreX = (int) ev.getX(); 
        centreY = (int) ev.getY(); 
        // findCirclePixels(); 
        TouchView.this.invalidate(); 
        break; 

      }   

      case MotionEvent.ACTION_UP: 

       break; 

     } 
     return true; 
    }//end of onTouchEvent 





    public void initSlider(final HorizontalSlider slider) 
    { 
     Log.e(TAG, "******setting up slider*********** "); 
     slider.setOnProgressChangeListener(changeListener); 

    } 



    private OnProgressChangeListener changeListener = new OnProgressChangeListener() { 


     @Override 
     public void onProgressChanged(View v, int progress) { 
      // TODO Auto-generated method stub 


       setProgress(progress); 
      //TouchView.this.Progress = progress; 


     } 
    }; 




    @Override 
    public void onDraw(Canvas canvas){ 
     super.onDraw(canvas); 

     Log.e(TAG, "++++++++++ inside ondraw"); 
     canvas.drawBitmap(bgr2, 0, 0, null); 
     // canvas.drawCircle(centreX, centreY, radius,pTouch); 

     Log.e(TAG, "++++++++++end ondraw"); 

    }//end of onDraw 




    protected void setProgress(int progress2) { 
     Log.e(TAG, "***********in SETPROGRESS"); 
     this.Progress = progress2; 



     findCirclePixels(); 
     invalidate(); 


    } 




} 

回答

1

位圖bgr2是在構造函數中創建的,但是沒有任何內容被繪製在其中。它將從findCirclePixels中被繪製,並在滑塊更改時被調用。

我覺得createBitmap (int width, int height, Bitmap.Config config)只使用配置,您可以使用其他變種創建位圖可能是

Bitmap createBitmap (Bitmap source, int x, int y, int width, int height) 
+0

嗨啊是愚蠢的忽略。我已經有畫布來繪製bgr位圖,但它仍然不會繪製它,直到我觸摸滑塊。我已經嘗試在創建bgr位圖後調用invalidate。不知道哪裏最好的地方叫無效 – turtleboy

+0

在活動中創建和添加視圖後,您可以直接發送invalidate或調用一個方法來使其無效? – Dileep

+0

mmm我已經嘗試在設置活動中的滑塊後調用invalidate,但在視圖膨脹後它仍然不顯示bgr位圖 – turtleboy

1

在onAttachedToWindow()中調用invalidate()可能嗎?

+0

有類似的問題。這個方法解決了它。 – FeatureCreep