如何禁用 invalidate()?

发布于 2025-01-07 10:39:31 字数 2763 浏览 3 评论 0原文

起初我的程序看起来像这样:

在此处输入图像描述

但是当我在屏幕上移动图片时,程序变成:

在此处输入图像描述

红色图是在 onDraw 方法中随机生成的。所以我想拖动图片并拥有在移动图片时不会失效的静态背景。

这是我的代码: 自定义视图: 包 com.graph.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import java.util.Random;

public class CustomView extends View
{
Random random=new Random();
public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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

public CustomView(Context context) {
    super(context);
}
public void onDraw (Canvas canvas)
{
    super.onDraw(canvas);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);
    Path path=new Path();
    path.moveTo(10, 10);
    for (float i=0.5f; i<=140; i+=10)
    {
        path.quadTo(10+i*5, 10+random.nextInt(500), 10+(i+10)*5,     10+random.nextInt(500));
    }
    paint.setDither(true);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);    
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(2);
    canvas.drawPath(path, paint);
}

}

主要活动

package com.graph.base;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends Activity {
FrameLayout frame;
ImageView image;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    frame=(FrameLayout)findViewById(R.id.frameLayout1);
    image=(ImageView)findViewById(R.id.imageView1);
    image.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.e("TAG", "Touch happend");
            switch (event.getAction())
            {
            case MotionEvent.ACTION_MOVE:
            {
                image.setPadding((int)event.getX(),     (int)event.getY(), 0, 0);
                break;
            }

            }

            return true;
        }
    });
}

public void invalidateButton_Click (View v)
{
    frame.invalidate();
}
}

At first my programm looks like this:

enter image description here

But when i am moving the picture around the screen programm turns to:

enter image description here

Red graph is random-generated in onDraw method. So i want to draging picture around and have static background that not inavalidates while im moving picture.

here is my code:
Custom View:
package com.graph.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import java.util.Random;

public class CustomView extends View
{
Random random=new Random();
public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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

public CustomView(Context context) {
    super(context);
}
public void onDraw (Canvas canvas)
{
    super.onDraw(canvas);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);
    Path path=new Path();
    path.moveTo(10, 10);
    for (float i=0.5f; i<=140; i+=10)
    {
        path.quadTo(10+i*5, 10+random.nextInt(500), 10+(i+10)*5,     10+random.nextInt(500));
    }
    paint.setDither(true);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);    
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(2);
    canvas.drawPath(path, paint);
}

}

MainActivity

package com.graph.base;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends Activity {
FrameLayout frame;
ImageView image;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    frame=(FrameLayout)findViewById(R.id.frameLayout1);
    image=(ImageView)findViewById(R.id.imageView1);
    image.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.e("TAG", "Touch happend");
            switch (event.getAction())
            {
            case MotionEvent.ACTION_MOVE:
            {
                image.setPadding((int)event.getX(),     (int)event.getY(), 0, 0);
                break;
            }

            }

            return true;
        }
    });
}

public void invalidateButton_Click (View v)
{
    frame.invalidate();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2025-01-14 10:39:31

您正在画布上绘制随机线条。每次屏幕失效时都会调用此代码。如果您想保持绘图不发生变化,请将位图设置到画布中并绘制到位图中。在 onDraw() 方法中,您将把位图绘制到画布上。

// Outside of onDraw()
Bitmap bmDest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas mycanvas = new Canvas(bmDest);
// Do your drawing on the canvas outside of the onDraw() method

在 onDraw() 中,您只需执行以下操作:

canvas.drawBitmap(bmDest, x, y, null);

You're drawing random lines onto your canvas. This code is called each time the screen is invalidated. If you want to keep the drawing from changing, then set a bitmap into a canvas and draw into the bitmap. In your onDraw() method you'll then draw the bitmap onto the canvas.

// Outside of onDraw()
Bitmap bmDest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas mycanvas = new Canvas(bmDest);
// Do your drawing on the canvas outside of the onDraw() method

In onDraw(), you'll simply do this:

canvas.drawBitmap(bmDest, x, y, null);
何其悲哀 2025-01-14 10:39:31

实际上,您不应该“禁用无效”,但确实应该调用“无效”...

case MotionEvent.ACTION_MOVE:
        {
            image.setPadding((int)event.getX(),     (int)event.getY(), 0, 0);
            // here call frame.invalidate(Rect dirty), with dirty set to where the image was previously
            break;
        }

注意,如果您的自定义视图“随机”绘制,那么您可以获得带有缓存版本的位图并从中绘制(位图复制)

Actually, you should not "disable invalidate" but really do call "invalidate"...

In

case MotionEvent.ACTION_MOVE:
        {
            image.setPadding((int)event.getX(),     (int)event.getY(), 0, 0);
            // here call frame.invalidate(Rect dirty), with dirty set to where the image was previously
            break;
        }

Note that if your custom View draws "Randomly", then you can get a Bitmap with the cached version and draw from that (blit copy)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文