如何强制invalidate()?(how to force invalidate()?)

我已经有了构成我的应用程序的以下类,它们将鱼眼失真放在位图上。 当我加载位图时,它只在触摸屏幕或滑动条时显示,并且在视图的构造函数完成后不显示位图。 我已经把一些日志记录在那里,基本上ondraw()被调用,但没有画到画布上。 我怎样才能让应用程序视图构造函数完成后直接失效?

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

i've got the following classes that make up my app that places a fisheye distortion on a bitmap. when i load the bitmap it ONLY displays when i touch the screen or slidebar and doesn't display the bitmap after the view's constructor has completed. i've put some logging in there and basically the ondraw() is called but nothing is drawn to the canvas. How can i get the app to invalidate straight after the view constructor completes?

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

最满意答案

位图bgr2是在构造函数中创建的,但没有绘制任何内容。 它将从findCirclePixels中被绘制,并在滑块更改时被调用。

我认为createBitmap (int width, int height, Bitmap.Config config)只使用config,可以使用其他变体创建位图,可能是

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

Bitmap bgr2 is created in constructor but nothing is painted in it. It will get painted from findCirclePixels which gets called on slider change.

I think createBitmap (int width, int height, Bitmap.Config config) only uses config, you can use other variant create bitmap may be

Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)如何强制invalidate()?(how to force invalidate()?)

我已经有了构成我的应用程序的以下类,它们将鱼眼失真放在位图上。 当我加载位图时,它只在触摸屏幕或滑动条时显示,并且在视图的构造函数完成后不显示位图。 我已经把一些日志记录在那里,基本上ondraw()被调用,但没有画到画布上。 我怎样才能让应用程序视图构造函数完成后直接失效?

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

i've got the following classes that make up my app that places a fisheye distortion on a bitmap. when i load the bitmap it ONLY displays when i touch the screen or slidebar and doesn't display the bitmap after the view's constructor has completed. i've put some logging in there and basically the ondraw() is called but nothing is drawn to the canvas. How can i get the app to invalidate straight after the view constructor completes?

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

最满意答案

位图bgr2是在构造函数中创建的,但没有绘制任何内容。 它将从findCirclePixels中被绘制,并在滑块更改时被调用。

我认为createBitmap (int width, int height, Bitmap.Config config)只使用config,可以使用其他变体创建位图,可能是

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

Bitmap bgr2 is created in constructor but nothing is painted in it. It will get painted from findCirclePixels which gets called on slider change.

I think createBitmap (int width, int height, Bitmap.Config config) only uses config, you can use other variant create bitmap may be

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