gpt4 book ai didi

android - onDraw() 未被调用

转载 作者:太空狗 更新时间:2023-10-29 16:42:30 26 4
gpt4 key购买 nike

我正在修复某人的 Android 应用程序。它最初是为 2.1 制作的,我正在努力让它工作到 4.0+。不幸的是,制作该应用程序的人不在身边,所以我只能使用他的代码。

该应用程序实现了自定义 slider 、水平 slider 和垂直 slider 。我固定了垂直 slider ,但我无法让水平 slider 工作。它没有调用 onDraw():当 View 加载时,onDraw() 被调用并且我看到我的两个 slider 都已初始化,然后当我移动我的水平 slider 时它移动并调用 onDraw() 一次,但它再也不会调用它,这与调用它的垂直 slider 不同。

代码如下:

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

这是有效的垂直 slider 代码:

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>

最佳答案

如果你正在扩展一个 ViewGroup,你应该覆盖 dispatchDraw() 而不是 onDraw(),或者在构造函数中调用setWillNotDraw(false)。有一些讨论here .

关于android - onDraw() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15394450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com