【问题标题】:Partial redraw -> invalidate (Rect rect)部分重绘 -> 无效(矩形矩形)
【发布时间】:2014-02-15 17:52:13
【问题描述】:

在我的 onDraw 中,我拥有构建整个视图所需的所有代码,但是如何检测我是否只想执行部分重绘。 我猜应该通过调用 canvas.invalidate(Rect rect); 来触发部分重绘。 正确的? 在我设备的开发者设置中,我启用了“显示屏幕更新”,但这总是告诉我我的整个屏幕都被重绘了……

下面是我的应用的截图:

如您所见,它是一个日历,我想在单击条目时给用户一个视觉反馈(假设周围有一个红色边框)......

我已经看过一些示例,但它们要么使用位图,要么使用大量成员变量来执行仅在 onDraw 中重绘特定区域所需的代码……

谁能告诉我实现此类功能的最佳方式是什么?

更新:

在我的第一次抽奖中,Canvas.getClipBounds() 返回以下矩形:

Rect(0, 0 - 1200, 1800)

当我调用invalidate(new Rect(304, 748 - 529, 902)) 并在onDraw() 中再次检查getClipBounds() 时,它仍然具有相同的值。

UPDATE2(我的代码):

@Override
public boolean onTouch(View v, MotionEvent event) {

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        _pTouchDown = new PointF(event.getX(), event.getY());

        downX = event.getX();
        downY = event.getY();

        entrySelected = hasTimeRecordAt(downX, downY);
        if (entrySelected != null) {
            Rect rInvalidate = new Rect((int) entrySelected.get_Selected().left, (int) entrySelected.get_Selected().top, (int) entrySelected.get_Selected().right,
                    (int) entrySelected.get_Selected().bottom);

            invalidate(rInvalidate);


        } 

        return false;
    }

更新 3(我的布局):

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MultiDayCalendarActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/rlStatusline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tvStatusline1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="asdf" >
            </TextView>

            <TextView
                android:id="@+id/tvStatusline2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="1234" >
            </TextView>
        </RelativeLayout>

        <com.mxp.time.calendar.DayHeader
                android:id="@+id/dayHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 />

        <ScrollView
            android:id="@+id/m_svMultiRoot1"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1" >

            <com.mxp.time.calendar.Calendar
                android:id="@+id/calendar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 />
        </ScrollView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@color/brushBackgroundLight" >
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/rlMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true" >

                <ImageButton
                    android:id="@+id/ibtCreateNewTimeRecord"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/menu" />

                <ImageButton
                    android:id="@+id/ibtCalendarStopwatch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/stopwatch" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ibtCalendarBack"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/previous" />

                <ImageButton
                    android:id="@+id/ibtCalendarForward"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/next" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true" >

                <ImageButton
                    android:id="@+id/ibtCalendarToday"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/today" />

                <ImageButton
                    android:id="@+id/ibtGotoJobs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/jobs" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start" >
    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

更新4:

setContentView(R.layout.test_calendar);
        // _cal = (Calendar) findViewById(R.id.calendar);
        _cal = new Calendar(this);

        _dayHeader = (DayHeader) findViewById(R.id.dayHeader);

        final ScrollView sv = (ScrollView) findViewById(R.id.m_svMultiRoot1);
        sv.addView(_cal);

同样的结果:

我在 onTouch 中传递 Rect(172, 748 - 265, 902),在 onDraw 中得到 Rect(0, 0 - 720, 1800)

更新 5:

package com.example.testclip;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

class V extends View {

private static final String TAG = "null";
Rect clip = new Rect();

public V(Context context) {
    super(context);
    int[] colors = { 0xff000000, 0xffff0000, 0xffffffff };
    Drawable d = new android.graphics.drawable.GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation.TOP_BOTTOM, colors);
    setBackgroundDrawable(d);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    int x = (int) event.getX();
    int y = (int) event.getY();
    StringBuilder sb = new StringBuilder();

    sb.append("left: ");
    sb.append(x);
    sb.append(", top: ");
    sb.append(y);

    sb.append("right: ");
    sb.append(x + 10);
    sb.append(", bottom: ");
    sb.append(y + 10);

    Log.d(TAG, "onTouchEvent  clip rect: " + sb.toString());

    invalidate(x, y, x + 10, y + 10);

    return false;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int w = MeasureSpec.getSize(widthMeasureSpec);
    setMeasuredDimension(w, w * 4);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.getClipBounds(clip);
    StringBuilder sb = new StringBuilder();

    sb.append("left: ");
    sb.append(clip.left);
    sb.append(", top: ");
    sb.append(clip.top);

    sb.append("right: ");
    sb.append(clip.right);
    sb.append(", bottom: ");
    sb.append(clip.bottom);

    Log.d(TAG, "onDraw  clip rect: " + sb.toString());
}
}

活动:

package com.example.testclip;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ScrollView;

public class TestClipMainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_test_clip_main);

     ScrollView sv = new ScrollView(this);
        V v = new V(this);
        sv.addView(v);
        setContentView(sv);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.test_clip_main, menu);
    return true;
}

}

此代码产生以下输出

02-15 10:47:54.011:D/OpenGLRenderer(833):启用调试模式 0 02-15 10:47:54.926: D/dalvikvm(833): threadid=1: 撤消后仍然挂起 (sc=1 dc=1) 02-15 10:48:03.806: D/null(833): onDraw 剪辑矩形:左:0,上:0 右:720,下:2880 02-15 10:48:05.381: D/null(833): onDraw 剪辑矩形:左:0,上:0 右:720,下:2880 02-15 10:48:07.181: D/null(833): onTouchEvent 剪辑矩形:左:409,上:358,右:419,下:368 02-15 10:48:09.806: D/null(833): onDraw 剪辑矩形:左:0,上:0,右:720,下:2880

【问题讨论】:

  • 你不能只获取你选择的矩形的位置并在它下面画一个矩形,每边多出 10px,所以它看起来像一个边框?
  • Canvas.getClipBounds ?
  • @iQue:这正是我所做的。实际上,我在 onDraw 方法之外计算了所有矩形,这样我就不需要在我的 onDraw 中分配任何对象。我有三个矩形:边框(黑色)、内部(条目的颜色)和选定的(红色的)。在触摸时,我检查坐标是否在任何矩形中,如果是,我执行重绘并使用“选定”矩形而不是我的“默认”黑色边框矩形。我只是不想每次都重绘一切......
  • @pskink getClipBounds 总是返回相同的值。我用这些信息更新了我的帖子
  • 你可以用Canvas.save()剪辑你想要的区域,只在这里画Canvas.restore()

标签: android performance android-layout


【解决方案1】:

你一定是做错了什么,看看这个自定义视图:

class V extends View {

    Rect clip = new Rect();
    private int cnt = 20;

    public V(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        cnt++;
        Log.d(TAG, "calling invalidate " + cnt);
        invalidate(10, 10, cnt, cnt);
        return false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.getClipBounds(clip);
        Log.d(TAG, "onDraw clip " + clip);
    }
}

更新:ScrollView 中的自定义视图:

class V extends View {

    Rect clip = new Rect();

    public V(Context context) {
        super(context);
        int[] colors = {0xff000000, 0xffff0000, 0xffffffff};
        Drawable d = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
        setBackgroundDrawable(d);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        invalidate(x, y, x + 10, y + 10);
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(w, w * 4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.getClipBounds(clip);
        Log.d(TAG, "onDraw clip height: " + clip.height());
    }
}

将此添加到 onCreate:

    ScrollView sv = new ScrollView(this);
    V v = new V(this);
    sv.addView(v);
    setContentView(sv);

【讨论】:

  • 不,抱歉!我在 getClipBounds() 之前调用 super.onDraw。也许这就是问题所在?打算测试一下
  • 不。我切换到我的三星 Note2 并调用 -> invalidate (Rect(172, 1219 - 265, 1383)) 我的 onDraw 的第一行是: Rect rCLip = canvas.getClipBounds();这个 rCLip 是 -> Rect(0, 0 - 720, 1800)。我用我的 onTouch 方法中的代码扩展了我的问题
  • 当您单击两次或陷入边界时,只有前两个 onDraws 报告完整视图边界
  • 我不会点击两次或陷入泥潭。我想我已经尝试了一千次点击 ^^ 我刚刚从我的 onTouch 中删除了所有代码并调用:invalidate(new Rect(10,10,10,10));在我的 onDraw 的第一行中,我做了 canvas.getClipBounds 但我没有得到任何剪辑。会不会因为我的自定义视图放在滚动视图中而导致此问题?
  • 第三次更新我的帖子,现在我添加了我的布局文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 2015-12-21
  • 2018-02-08
  • 1970-01-01
相关资源
最近更新 更多