【问题标题】:Android: View onDraw never calledAndroid:View onDraw 从未调用过
【发布时间】:2015-02-22 23:25:58
【问题描述】:

(解决方法)可以在我的问题末尾找到解决方案

我已经搜索了几个小时,但找不到任何可以解决我的问题的方法。

我有一个自定义视图,它从一个点到另一个点画一条线。 然后将该视图添加到根视图中。

MainActivity.java

package be.serafijnboelaert.lapse.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import be.serafijnboelaert.lapse.R;


public class MainActivity extends Activity {

    private RelativeLayout rl;
    private GridLayout glActionMenu;
    private int width;


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

        rl = (RelativeLayout) this.findViewById(R.id.mainContent);

        rl.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rl.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setWidth(rl.getMeasuredWidth());
            }
        });


    }

    private void setWidth(int width) {
        this.width = width;


        glActionMenu = (GridLayout) this.findViewById(R.id.glActionMenu);


        for(int i = 0 ; i < glActionMenu.getChildCount() ;i++) {
            GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
            lp.width = width / 5;
            lp.height = lp.width;

            if((i >= 5 && i < 10) || (i >= 15 && i< 20)) {
                lp.height = lp.width / 2 + lp.width / 4;
            }
            glActionMenu.getChildAt(i).setLayoutParams(lp);
        }
        MyCustomView myCustomView;

        glActionMenu.getChildAt(glActionMenu.getChildCount() -1).addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                Toast.makeText(v.getContext(), "hello",Toast.LENGTH_LONG).show();

                ImageButton ib1 = (ImageButton) findViewById(R.id.ibOpenPictureActivity);

                ib1.setBackgroundColor(Color.RED);
                ImageButton ib2 = (ImageButton) findViewById(R.id.ibOpenAlbumActivity);
                ib2.setBackgroundColor(Color.GREEN);

                glActionMenu.setBackgroundColor(Color.BLUE);

                float fromX = ib1.getLeft() +ib1.getWidth() + rl.getLeft();
                float fromY = ib1.getTop() + ib1.getHeight()/2 + glActionMenu.getTop();
                float toX =  ib2.getLeft();
                float toY = ib2.getTop() + ib2.getHeight()/2;

                MyCustomView customView = new MyCustomView(v.getContext(), fromX, fromY, toX,toY);
                rl.addView(customView);
            }
        });
    }
}

MyCustomView.java

    package be.serafijnboelaert.lapse.activity;

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

/**
 * Created by Samsung on 25/12/2014.
 */
public class MyCustomView extends View {

    private float fromX;
    private float fromY;
    private float toX;
    private float toY;

    public MyCustomView(Context context, float fromX, float fromY, float toX, float toY) {
        super(context);
        this.fromX = fromX;
        this.fromY = fromY;
        this.toX = toX;
        this.toY = toY;
    }

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

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        canvas.drawLine(fromX,fromY,toX,toY, paint);
    }

    @Override
    public void onMeasure(int wms, int hms){
        setMeasuredDimension(MeasureSpec.getSize(wms), MeasureSpec.getSize(hms));
    }
}

activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:id="@+id/mainContent">


    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/action_menu"
        android:layout_gravity="center"
        android:layout_centerInParent="true" />
</RelativeLayout>

action_menu.xml

<?xml version="1.0" encoding="utf-8"?>



<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:columnCount="5"
    android:rowCount="5"
    android:columnOrderPreserved="false"
    android:rowOrderPreserved="true"
    android:id="@+id/glActionMenu" >

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="0"
        android:layout_row="0" />

    <ImageButton
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/ibOpenPictureActivity"
        android:scaleType="fitXY"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_picture"
        android:layout_column="1"
        android:layout_row="0"/>

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="2"
        android:layout_row="0" />

    <ImageButton
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/ibOpenAlbumActivity"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_album"
        android:layout_column="3"
        android:layout_row="0" />

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="4"
        android:layout_row="0" />

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="0"
        android:layout_row="1" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="1"
        android:layout_row="1" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="2"
        android:layout_row="1" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="3"
        android:layout_row="1" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="4"
        android:layout_row="1" />

    <ImageButton
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/ibOpenLapseActivity"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_lapse"
        android:layout_column="0"
        android:layout_row="2" />

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="1"
        android:layout_row="2" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="2"
        android:layout_row="2" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="3"
        android:layout_row="2" />

    <ImageButton
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/ibOpenSettingsActivity"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_settings"
        android:layout_column="4"
        android:layout_row="2" />

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="0"
        android:layout_row="3" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="1"
        android:layout_row="3" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="2"
        android:layout_row="3" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="3"
        android:layout_row="3" />
    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="4"
        android:layout_row="3" />

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="0"
        android:layout_row="4" />

    <ImageButton
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btnOpenShareActivity"
        android:scaleType="fitXY"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_share"
        android:layout_column="1"
        android:layout_row="4" />

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="2"
        android:layout_row="4" />

    <ImageButton
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/ibOpenHelpActivity"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_help"
        android:layout_column="3"
        android:layout_row="4" />

    <View
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_column="4"
        android:layout_row="4" />


</GridLayout>

问题是,我的 customview 中的 onDraw 方法从未被调用,但其他布局更改(绿色和红色的按钮以及蓝色的 gridlayout)工作正常..

我需要在操作按钮的布局更改后绘制该线,因为该线只是将两个操作按钮相互连接。

我知道,我在每个按钮上都设置了一个 GlobalLayoutChangeListerer,这没什么用,但那是为了测试目的。

我真的希望我能在这里找到解决方案。

提前谢谢!

(解决方法)解决方案

package be.serafijnboelaert.lapse.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import be.serafijnboelaert.lapse.R;


public class MainActivity extends Activity {

    private RelativeLayout rl;
    private GridLayout glActionMenu;
    private int width;

    private float fromX;
    private float fromY;
    private float toX;
    private float toY;


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

        rl = (RelativeLayout) this.findViewById(R.id.mainContent);

        rl.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rl.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setWidth(rl.getMeasuredWidth());
            }
        });


    }

    private void setWidth(int width) {
        this.width = width;


        glActionMenu = (GridLayout) this.findViewById(R.id.glActionMenu);


        for(int i = 0 ; i < glActionMenu.getChildCount() ;i++) {
            GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
            lp.width = width / 5;
            lp.height = lp.width;

            if((i >= 5 && i < 10) || (i >= 15 && i< 20)) {
                lp.height = lp.width / 2 + lp.width / 4;
            }
            glActionMenu.getChildAt(i).setLayoutParams(lp);
        }

        glActionMenu.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                ImageButton ib1 = (ImageButton) findViewById(R.id.ibOpenPictureActivity);
                ImageButton ib2 = (ImageButton) findViewById(R.id.ibOpenAlbumActivity);

                fromX = ib1.getRight() + rl.getLeft();
                fromY = ib1.getTop() + ib1.getHeight()/2 + glActionMenu.getTop();
                toX =  ib2.getLeft();
                toY = ib2.getTop() + ib2.getHeight()/2 + glActionMenu.getTop();
            }
        });

        glActionMenu.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                MyCustomView customView = new MyCustomView(MainActivity.this, fromX, fromY, toX, toY);
                rl.addView(customView);
            }
        }, 10);
    }
}

【问题讨论】:

  • 尝试在 MyCustomView 构造函数中调用 setWillNotDraw (false)
  • 能否在 MyCustomView 的构造函数和 onDraw 方法中放置一些日志语句来确认它们是否被调用?
  • glActionMenu.postDelayed(new Runnable() { @Override public void run() { customView = new MyCustomView(MainActivity.this, fromX, fromY, toX, toY); rl.addView(customView); } }, 10);
  • 构造函数被调用,但 onDraw 方法不被调用。我已经放置了一些断点来测试它。

标签: android android-custom-view ondraw


【解决方案1】:

正如this 回答所说:onDraw() is called each time the view needs to be drawn. When the view is off screen then onDraw() is never called.

您确定您的Views 确实可见吗?顺便说一句,尝试为该视图设置一个可行的背景以确定它们是否显示。

MyCustomView customView = new MyCustomView(...);
// set the background to green
customView.setBackgroundColor(0x0000FF00);
rl.addView(customView);

【讨论】:

  • 我已将背景色设置为黄色,视图不可见
【解决方案2】:

试试这个代码为我工作。

public class MainActivity extends ActionBarActivity
{
    private RelativeLayout rl;
    private GridLayout glActionMenu;
    private int width;
    private MyCustomView customView;
    private int fromX;
    private int fromY;
    private int toX;
    private int toY;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // MyCustomView customView = new MyCustomView(this, 0, 0, 100,200);
        // setContentView(customView);

        setContentView(R.layout.activity_main);
        rl = (RelativeLayout) this.findViewById(R.id.mainContent);

        // RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(500, 500);
        // customView = new MyCustomView(MainActivity.this, 0, 0, 100, 200);
        // customView.setBackgroundColor(Color.DKGRAY);
        // rl.addView(customView);

        rl.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
        {
            @SuppressLint("NewApi")
            @Override
            public void onGlobalLayout()
            {
                rl.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setWidth(rl.getMeasuredWidth());
            }
        });
        // setWidth(rl.getMeasuredWidth());
    }

    private void setWidth(int width)
    {
        this.width = width;

        glActionMenu = (GridLayout) this.findViewById(R.id.glActionMenu);

        for (int i = 0; i < glActionMenu.getChildCount(); i++)
        {
            GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
            lp.width = width / 5;
            lp.height = lp.width;

            if ((i >= 5 && i < 10) || (i >= 15 && i < 20))
            {
                lp.height = lp.width / 2 + lp.width / 4;
            }
            glActionMenu.getChildAt(i).setLayoutParams(lp);
        }

        glActionMenu.getChildAt(glActionMenu.getChildCount() - 1).addOnLayoutChangeListener(new View.OnLayoutChangeListener()
        {

            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
            {
                Toast.makeText(v.getContext(), "hello", Toast.LENGTH_LONG).show();

                ImageButton ib1 = (ImageButton) findViewById(R.id.ibOpenPictureActivity);

                ib1.setBackgroundColor(Color.RED);
                ImageButton ib2 = (ImageButton) findViewById(R.id.ibOpenAlbumActivity);
                ib2.setBackgroundColor(Color.GREEN);

                glActionMenu.setBackgroundColor(Color.WHITE);

                fromX = ib1.getLeft() + ib1.getWidth() + rl.getLeft();
                fromY = ib1.getTop() + ib1.getHeight() / 2 + glActionMenu.getTop();
                toX = ib2.getLeft();
                toY = ib2.getTop() + ib2.getHeight() / 2;

                // customView = new MyCustomView(MainActivity.this, 0, 0, 100, 200);
                // customView.setBackgroundColor(Color.DKGRAY);
                // rl.addView(customView);
                // customView.invalidate();
            }

        });
        glActionMenu.postDelayed(new Runnable()
        {

            @Override
            public void run()
            {
                customView = new MyCustomView(MainActivity.this, fromX, fromY, toX, toY);
                rl.addView(customView);

            }
        }, 10);
    }
}

【讨论】:

  • 很好,这个解决方案有效!使用 postdelayed 方法不是一种解决方法吗?我认为这就像在猜测 UI 何时绘制还是我错了?
  • @Serafijn 不明白你的问题。如果有任何查询再次解释?
  • 我希望我能让我自己理解。如果我理解您的回答,我们会在该行代码 (glActionMenu.postDelayed(new Runnable()..) 为红色之后将 CustomView 的创建延迟 10 毫秒。实际上这将始终有效,因为 UI 已完全绘制大约 10 毫秒,但没有一个简洁的解决方案。类似于在 UI 完全绘制时触发的方法,而不是通过设置一个具有 10 毫秒延迟的可运行对象来猜测?
  • @Serafijn 找到解决方案?代码共享。
  • 在我的问题末尾添加了解决方案
猜你喜欢
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多