Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义视图未执行onDraw()_Android_Ondraw_Invalidation_Custom View - Fatal编程技术网

Android 自定义视图未执行onDraw()

Android 自定义视图未执行onDraw(),android,ondraw,invalidation,custom-view,Android,Ondraw,Invalidation,Custom View,我已经创建了一个名为CustomViewVisual1的自定义视图,它在类粒子的屏幕对象上绘制。我在名为LayoutCanvas的类中使用此自定义视图,该类扩展了活动。LayoutCanvas中的onTouch()或onProgressChanged()事件似乎都不会导致我的视图重新绘制,即使我调用了invalidate并尝试了postInvalidate()。我的对象的值会改变,但我不会重新绘制! 老实说,我是Android新手,不知道在如何设计我的类以及在哪里实现什么方面有什么好的实践,所以

我已经创建了一个名为CustomViewVisual1的自定义视图,它在类粒子的屏幕对象上绘制。我在名为LayoutCanvas的类中使用此自定义视图,该类扩展了活动。LayoutCanvas中的onTouch()或onProgressChanged()事件似乎都不会导致我的视图重新绘制,即使我调用了invalidate并尝试了postInvalidate()。我的对象的值会改变,但我不会重新绘制! 老实说,我是Android新手,不知道在如何设计我的类以及在哪里实现什么方面有什么好的实践,所以如果有人对我要实现的目标有更好的解决方案,请告诉我。提前谢谢

这是粒子类:

    public class Particle {
    Rect rect = new Rect();
    float similarity;
    Point p = new Point();
    float x;
    float y;

    float tx = 240;
    float ty = 320;

    Matrix transform = new Matrix();

    float r;
    Paint _paint = new Paint();
    Paint tempp = new Paint();

    Particle(){}

    Particle(int width, float similarity){
        this.similarity = similarity;
        x = MyMath.map(similarity, 0, 1, 0, width/2);
        //y = MyMath.myRandom(-20, 20);
        y = 0;
        //p.set((int) MyMath.map(similarity, 0, 1, 0, width/2), (int)MyMath.myRandom(0, 360));
        p.set((int)x, (int)y);
        r = MyMath.myRandom(0, 360);
        //recalculateCoordinates();
        _paint.setColor(Color.WHITE);
        tempp.setColor(Color.GREEN);
        //rotate(r);

    }


    public void display(Canvas canvas, Paint paint, float scale){
        //canvas.drawLine(canvas.getWidth()/2, 0, canvas.getWidth()/2, canvas.getHeight(), _paint);
        //canvas.drawLine(0, canvas.getHeight()/2, canvas.getWidth(), canvas.getHeight()/2, _paint);
        canvas.save();
        canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
        //canvas.translate(tx, ty);
        canvas.rotate(r);
        //canvas.scale(scale, scale);
        paint.setColor(colorize());
        canvas.drawRect(-p.x, p.y, -p.x+10, p.y+10, paint);
        canvas.restore();

        canvas.save();
        canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
        canvas.drawLine(0, 0, 200, 0, tempp);
        canvas.drawText("0'", 100, 8, tempp);
        canvas.restore();

        canvas.save();
        canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
        canvas.rotate(45);
        canvas.drawLine(0, 0, 200, 0, tempp);
        canvas.drawText("45''", 100, 8, tempp);
        canvas.restore();

        canvas.save();
        canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
        canvas.rotate(90);
        canvas.drawLine(0, 0, 200, 0, tempp);
        canvas.drawText("90'", 100, 8, tempp);
        canvas.restore();

        canvas.save();
        canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
        canvas.rotate(180);
        canvas.drawLine(0, 0, 200, 0, tempp);
        canvas.drawText("180'", 100, 8, tempp);
        canvas.restore();
    }

    public int colorize(){
        return Color.argb(100, Math.round(255-MyMath.map(similarity, 0, 1, 0, 255)), 0, Math.round(MyMath.map(similarity, 0, 1, 0, 255)));
    }

    public void rotate(float degrees){
        transform.setTranslate(120, 160);
        transform.setRotate(degrees, 0, 0);

        // Create new float[] to hold the rotated coordinates
        float[] pts = new float[2];

        // Initialize the array with our Coordinate
        pts[0] = p.x;
        pts[1] = p.y;

        // Use the Matrix to map the points
        transform.mapPoints(pts);

        p.set((int)pts[0], (int)pts[1]);
        // NOTE: pts will be changed by transform.mapPoints call
        // after the call, pts will hold the new cooridnates

        // Now, create a new Point from our new coordinates
        //Point newPoint = new Point((int)pts[0], (int)pts[1]);

        // Return the new point
        //p.set(newPoint.x, newPoint.y);
        //return newPoint;
    }

    public void translate(int x, int y){
        tx = x;
        ty = y;
    }

    public void recalculateCoordinates(){
        x = (float) ( (x * android.util.FloatMath.cos(r) ) - (y * android.util.FloatMath.sin(r) ) );
        y = (float) ( (x * android.util.FloatMath.sin(r) ) - (y * android.util.FloatMath.cos(r) ) );
    }
    /**
     * @return the similarity
     */
    public float getSimilarity() {
        return similarity;
    }

    /**
     * @param similarity the similarity to set
     */
    public void setSimilarity(float similarity) {
        this.similarity = similarity;
    }

    /**
     * @return the x
     */
    public float getX() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(float x) {
        this.x = x;
    }

    /**
     * @return the y
     */
    public float getY() {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(float y) {
        this.y = y;
    }

    /**
     * @return the r
     */
    public float getR() {
        return r;
    }

    /**
     * @param r the r to set
     */
    public void setR(float r) {
        this.r = r;
    }

    public Point getP() {
        return p;
    }

    public void setP(Point p) {
        this.p = p;
    }
    }
这是CustomViewVisual1类:

    public class CustomViewVisual1 extends View implements Runnable{
    int width;
    int height;
    int x = 0; 
    int y = 0;
    int canvasScale = 1;
    int countDraw = 0;

    Thread t = null;
    boolean isItOK = false;

    Paint paint;
    Particle[] p = new Particle[500];

    public CustomViewVisual1(Context context){
        super(context);
        initView();
    }


    public CustomViewVisual1(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        paint = new Paint();

        Random r = new Random();
        for (int i = 0; i < p.length; i++) {
            p[i] = new Particle(width, r.nextFloat());
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        x = width/2;
        y = height/2;
        initView();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.translate(width/2, height/2);
        canvas.drawARGB(255, 0, 0, 0);
        canvas.scale(canvasScale, canvasScale);
        for (int i = 0; i < p.length; i++) {
            p[i].display(canvas, paint, canvasScale);
        }

        paint.setColor(Color.WHITE);
        canvas.drawCircle(x, y, 20, paint);
        /*
        paint.setColor(Color.CYAN);
        canvas.drawRect(0, 0, width, 10, paint);
        paint.setColor(Color.GREEN);
        canvas.drawRect(0, height-10, width, height, paint);
        paint.setColor(Color.YELLOW);
        canvas.drawRect(0, 0, 10, height, paint);
        paint.setColor(Color.MAGENTA);
        canvas.drawRect(width-10, 0, width, height, paint);
        */
        countDraw++;
        Log.d("How many draw = ", ""+countDraw);
    }


    public float getCanvasScale() {
        return canvasScale;
    }


    public void setCanvasScale(int canvasScale) {
        this.canvasScale = canvasScale;
    }

    public void pause() {
        isItOK = false;
        while (true) {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
        t = null;
    }

    public void resume() {
        isItOK = true;
        t = new Thread(this);
        t.start();
    }


    @Override
    public void run() {
        // TODO Auto-generated method stub
    }
    }
public类CustomViewVisual1扩展视图实现可运行{
整数宽度;
内部高度;
int x=0;
int y=0;
int canvasScale=1;
int countDraw=0;
线程t=null;
布尔值isItOK=false;
油漆;
粒子[]p=新粒子[500];
公共CustomViewVisual1(上下文){
超级(上下文);
initView();
}
公共CustomViewVisual1(上下文、属性集属性){
超级(上下文,attrs);
initView();
}
私有void initView(){
//TODO自动生成的方法存根
油漆=新油漆();
随机r=新随机();
for(int i=0;i
这是LayoutCanvas类:

    public class LayoutCanvas extends Activity implements OnTouchListener{

    RelativeLayout rl;
    SeekBar sb;
    CustomViewVisual1 cView;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_layout_canvas);

        cView = new CustomViewVisual1(this);
        cView.setOnTouchListener(this);

        rl = (RelativeLayout) findViewById(R.id.lay_canvas);
        rl.setOnTouchListener(this);
        rl.setOnLongClickListener(new OnLongClickListener(){
            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                Log.d("on long touch", "keep touching");
                return false;
            }});

        sb = (SeekBar) findViewById(R.id.sb_activity_layout_canvas);
        sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.d("in progress changed", "before if() "+progress);
                if(progress > 0 && progress <=10){ 
                    Log.d("in progress changed", "in if() " + progress);
                    cView.setCanvasScale(progress);
                    cView.invalidate();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }});

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        Log.d("on touch", "Just touched");
        cView.x = (int) event.getX();
        cView.y = (int) event.getY();
        Log.d("cView x = ", ""+cView.x);
        Log.d("cView y = ", ""+cView.y);
        cView.invalidate();
        return false;
    }



    }
公共类LayoutCanvas扩展活动实现到TouchListener上{
相对寿命;
SeekBar某人;
CustomViewVisual1 cView;
/*
*(非Javadoc)
* 
*@see android.app.Activity#onCreate(android.os.Bundle)
*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
setContentView(R.layout.activity\u layout\u canvas);
cView=新的CustomViewVisual1(此);
cView.setOnTouchListener(本);
rl=(RelativeLayout)findviewbyd(R.id.lay_canvas);
rl.setOnTouchListener(本);
rl.setOnLongClickListener(新的OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
//TODO自动生成的方法存根
Log.d(“长时间接触”、“保持接触”);
返回false;
}});
sb=(SeekBar)findviewbyd(R.id.sb\u活动\u布局\u画布);
sb.setonseekbarchaneglistener(新的onseekbarchaneglistener(){
@凌驾
public void onProgressChanged(SeekBar-SeekBar、int-progress、boolean-fromUser){
Log.d(“正在更改”、“在if()之前”+进度);

如果(progress>0&&progress正常,问题是您有两个自定义视图实例。第一个实例是在调用
setContentView(R.layout.activity\u layout\u canvas);
时创建的,布局文件被膨胀。此实例被添加到视图层次结构中。第二个实例是您显式创建的

cView = new CustomViewVisual1(this);
cView.setOnTouchListener(this);
但是,此视图从未添加到视图层次结构中。因此,当在
onTouch
中调用
cView.invalidate();
时,它不会绘制,因为
cView
实际上不在屏幕上

您需要做的是使
cView
引用使用如下布局膨胀的自定义视图:

cView = (CustomViewVisual1)findViewById(R.id.myView1);

“onTouch()或onProgressChanged()事件似乎都不会导致我的视图重新绘制”。似乎会导致?您的CustomViewVisual1.onDraw方法是否被调用?我在onDraw()中使用变量countDraw++;和Log.d(“How Number draw=”,“”+countDraw);并且除了一些初始值(如2-4)之外,不会获得新的计数
cView = (CustomViewVisual1)findViewById(R.id.myView1);