onDraw()没有被调用

时间:2013-03-13 19:15:05

标签: android

我正在努力修复某人的Android应用。它最初是为2.1制作的,我试图让它工作到4.0+。不幸的是,制作应用程序的人不在身边,所以我对他的代码感到困惑。

该应用程序实现自定义滑块,水平和垂直。我修复了垂直滑块,但我无法使水平滑块工作。它没有调用onDraw():当视图加载时,onDraw()被调用,我看到我的两个滑块都被初始化,然后当我移动我的水平滑块时,它移动并调用onDraw()一次,但是永远不会再称呼它,不像垂直滑块那样称呼它。

以下是代码:

public class PanSeekBar extends RelativeLayout {

    private RelativeLayout layout1;
    public ImageView track;
    public ImageView thumb;

    public boolean initialized = false;
    public int thumbDownPosX = 0;
    public int thumbDownPosY = 0;
    public int thumbLeft = 0;
    public int thumbTop = 0;
    public int thumbRight = 0;
    public int thumbBottom = 0;
    private int max = 100;

    public boolean touched = false;

    public PanSeekBar(Context context) {
        super(context);
        initialize(context);
        // TODO Auto-generated constructor stub
    }// End of PanSeekBar Constructor

    private void initialize(Context context) {
        Log.d("initialize (PanSeekBar)", "initialize");
        setWillNotDraw(false);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view1 = inflater.inflate(R.layout.pan_seekbar, this);

        layout1 = (RelativeLayout) view1.findViewById(R.id.ps_layout1);
        track = (ImageView) layout1.findViewById(R.id.ps_track);
        thumb = (ImageView) layout1.findViewById(R.id.ps_thumb);

        thumb.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                initialized = true;

                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.d("onTouch (PanSeekBar)", "Action Down");
                    thumbDownPosX = (int) event.getX();
                    thumbDownPosY = (int) event.getY();

                    thumbLeft = thumb.getLeft();
                    thumbTop = thumb.getTop();
                    thumbRight = thumb.getRight();
                    thumbBottom = thumb.getBottom();
                    touched = true;
                    break;

                case MotionEvent.ACTION_MOVE:
                    // Log.d("onTouch (PanSeekBar)", "Action Move");
                    int left = (int) (thumb.getLeft() + event.getX() - thumbDownPosX);
                    if (left < 0) {
                        left = 0;
                        Log.d("onTouch (PanSeekBar)", "left < 0: " + left);
                    } else if (left >= track.getWidth() - thumb.getWidth()) {
                        left = track.getWidth() - thumb.getWidth();
                        Log.d("onTouch (PanSeekBar)",
                                "left >= track.getWidth(): " + left);
                    }
                    int top = thumb.getTop();
                    int right = left + thumb.getWidth();
                    int bottom = top + thumb.getHeight();

                    thumbLeft = left;
                    thumbTop = top;
                    thumbRight = right;
                    thumbBottom = bottom;

                    Log.d("onTouch (PanSeekBar)",
                            "Action Move -- thumb layout: " + thumbLeft + ", "
                                    + thumbTop + ", " + thumbRight + ", "
                                    + thumbBottom);

                    layout1.invalidate();
                    // thumb.invalidate();

                    break;

                case MotionEvent.ACTION_UP:
                    Log.d("onTouch (PanSeekBar)", "Action Up");
                    touched = false;
                    break;
                }// End of switch

                return true;
            }// End of onTouch
        });
    }// End of initialize

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d("onDraw (PanSeekBar)", "onDraw");
        super.onDraw(canvas);

        if (initialized) {
            thumb.layout(thumbLeft, thumbTop, thumbRight, thumbBottom);
            Log.d("onDraw (PanSeekBar)", "thumb layout: " + thumbLeft + ", "
                    + thumbTop + ", " + thumbRight + ", " + thumbBottom);
        }
    }// End of onDraw
}// End of PanSeekBar

以下是可行的垂直滑块的代码:

public class MySeekBar extends RelativeLayout {

    private RelativeLayout layout1;
    public ImageView track;
    public ImageView thumb;
    private MyToast toast;
    private MyToast toast2;
    private MyToast toast3;
    private int globalMaxValue;
    private int globalProgress;

    private boolean initialized = false;
    public int thumbDownPosX = 0;
    public int thumbDownPosY = 0;
    public int thumbLeft = 0;
    public int thumbTop = 0;
    public int thumbRight = 0;
    public int thumbBottom = 0;

    private int globalSetProgress = 0;
    private int globalConfigType = 1;

    // Margin top in custom_seekbar.xml
    private int marginTop = 20;
    public boolean touched = false;

    public MySeekBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        initialize(context);
    }// End of MySeekBar

    public void initialize(Context context) {
        Log.d("initialize (MySeekBar)", "initialize");
        setWillNotDraw(false);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view1 = inflater.inflate(R.layout.custom_seekbar, this);

        layout1 = (RelativeLayout) view1.findViewById(R.id.al_cs_layout1);
        track = (ImageView) layout1.findViewById(R.id.cs_track);
        thumb = (ImageView) layout1.findViewById(R.id.cs_thumb);

        // Gets marginTop
        MarginLayoutParams params = (MarginLayoutParams) track
                .getLayoutParams();
        Log.d("initialize (MySeekBar)", "current margintop = " + marginTop);
        marginTop = params.topMargin;
        Log.d("initialize (MySeekBar)", "new margintop = " + marginTop);

        // int marginTopPixelSize = params.topMargin;
        // Log.d("initialize (MySeekBar)","debug = "+marginTopPixelSize);

        // Jared
        // When creating MyToast, setting width causes issues.
        toast = new MyToast(context);
        toast.setText("");
        toast.setBackgroundColor(Color.LTGRAY);
        toast.setTextColor(Color.BLACK);
        toast.setPadding(1, 1, 1, 1);
        toast.setVisibility(View.INVISIBLE);
        toast.setTextSize(10f);
        // toast.setWidth(40);

        toast2 = new MyToast(context);
        toast2.setTextSize(12f);
        toast2.setTextColor(Color.rgb(192, 255, 3));
        // toast2.setWidth(40);
        toast2.setPadding(10, 1, 1, 1);
        // toast2.setBackgroundColor(Color.BLACK);
        toast2.setVisibility(View.INVISIBLE);

        toast3 = new MyToast(context);
        toast3.setText("CH");
        toast3.setTextSize(15f);
        toast3.setTextColor(Color.rgb(192, 255, 3));
        // toast3.setWidth(40);
        toast3.setPadding(1, 1, 1, 1);

        layout1.addView(toast);
        layout1.addView(toast2);
        layout1.addView(toast3);

        thumb.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                initialized = true;

                // 0=down 1=up 2=move
                // Log.d("onTouch (MySeekBar)",
                // Integer.toString(event.getAction()));
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.d("onTouch (MySeekBar)", "Action Down");
                    thumbDownPosX = (int) event.getX();
                    thumbDownPosY = (int) event.getY();
                    // Position of thumb touched. Irrelevant to scale
                    // Log.d("onTouch (NewSeekBar)","thumbDownPosX "+thumbDownPosX+" thumbDownPosY "+thumbDownPosY);
                    thumbLeft = thumb.getLeft();
                    thumbTop = thumb.getTop();
                    thumbRight = thumb.getRight();
                    thumbBottom = thumb.getBottom();
                    touched = true;
                    toast2.setVisibility(View.INVISIBLE);
                    toast3.setVisibility(View.INVISIBLE);
                    toast.setVisibility(View.VISIBLE);
                    // Location/size of the thumb button
                    // Log.d("onTouch (NewSeekBar)","thumbLeft "+thumbLeft+" thumbRight "+thumbRight+" thumbTop "+thumbTop+" thumbBottom "+thumbBottom);

                    break;

                case MotionEvent.ACTION_MOVE:
                    // Log.d("onTouch (MySeekBar)", "Action Move");
                    int thumbHeight = thumb.getHeight();
                    int thumbWidth = thumb.getWidth();
                    int trackWidth = track.getWidth();
                    int trackHeight = track.getHeight();
                    // Location/size of the thumb button and track
                    // Log.d("onTouch (NewSeekBar)","thumbHeight "+thumbHeight+" thumbWidth "+thumbWidth+" trackWidth "+trackWidth+" trackHeight "+trackHeight);

                    int top = (int) (thumb.getTop() + event.getY() - thumbDownPosY);
                    Log.d("onTouch (NewSeekBar)", "top " + top);

                    // Top border
                    if (top < marginTop) {
                        top = marginTop;
                        Log.d("onTouch (NewSeekBar)", "top < marginTop " + top);
                    } else if (top >= trackHeight - thumbHeight + marginTop) {
                        top = trackHeight - thumbHeight + marginTop;
                        Log.d("onTouch (NewSeekBar)", "top >= trackHeight "
                                + top);
                    }

                    thumbLeft = thumb.getLeft();
                    ;
                    thumbTop = top;
                    thumbRight = thumbLeft + thumbWidth;
                    thumbBottom = top + thumbHeight;

                    // Log.d("onTouch (MySeekBar)",
                    // "thumbTop: "+thumbTop+" thumbBottom: "+thumbBottom);
                    int value = getProgress();
                    Log.d("onTouch (NewSeekBar)", "getProgress(): " + value);
                    // setdBText(value);

                    // Log.d("onTouch (MySeekBar)", "value: "+value);
                    setProgress(value);
                    // toast.setText(String.valueOf(temp));
                    // toast2.setText(String.valueOf(temp));
                    layout1.invalidate();

                    break;

                case MotionEvent.ACTION_UP:
                    Log.d("onTouch (MySeekBar)", "Action Up");
                    toast.setVisibility(View.INVISIBLE);
                    toast2.setVisibility(View.VISIBLE);
                    toast3.setVisibility(View.VISIBLE);
                    touched = false;
                    break;
                }// End of switch
                return true;
            }// End of onTouch
        });
    }// End of initialize

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d("onDraw (MySeekBar)", "onDraw");
        super.onDraw(canvas);

        if (initialized) {
            thumb.layout(thumbLeft, thumbTop, thumbRight, thumbBottom);
            Log.d("onDraw (MySeekBar)", "thumb layout: " + thumbLeft + ", "
                    + thumbTop + ", " + thumbRight + ", " + thumbBottom);
            int top = thumbTop - marginTop;
            int left = thumbLeft + 4;
            // toast.layout(left, top, left + toast.getWidth(), top +
            // toast.getHeight());

            top = thumbTop + thumb.getHeight() / 2 - toast2.getHeight() / 2 + 1;
            left = thumbLeft;
            toast2.layout(left, top, left + toast2.getWidth(),
                    top + toast2.getHeight());
        }
    }// End of onDraw

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/test_layout">

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

        <com.networksound.Mixer3.NewSeekBar
            android:id="@+id/newSeekBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </com.networksound.Mixer3.NewSeekBar>

        <com.networksound.Mixer3.NewSeekBar
            android:id="@+id/newSeekBar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </com.networksound.Mixer3.NewSeekBar>

        <com.networksound.Mixer3.MySeekBar
            android:id="@+id/mySeekBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </com.networksound.Mixer3.MySeekBar>

        <com.networksound.Mixer3.MySeekBar
            android:id="@+id/mySeekBar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </com.networksound.Mixer3.MySeekBar>

    </LinearLayout>

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

        <com.networksound.Mixer3.PanSeekBar
            android:id="@+id/panSeekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </com.networksound.Mixer3.PanSeekBar>

        <com.networksound.Mixer3.PanSeekBar
            android:id="@+id/panSeekBar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </com.networksound.Mixer3.PanSeekBar>

    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:5)

如果您要延长ViewGroup,则应覆盖dispatchDraw()而不是onDraw(),或者在构造函数中调用setWillNotDraw(false)。对它进行了一些讨论here