gpt4 book ai didi

android - ondraw 方法不起作用

转载 作者:行者123 更新时间:2023-11-29 22:06:27 26 4
gpt4 key购买 nike

我正在处理我的 ondraw 方法无法正常工作的应用程序......在我的应用程序中,我正在使用 framelayout ...所以我用了三级...1)活跃度2)图像选择3)绘图

我的图像选择工作正常,但我的绘图类工作不正常..它没有显示任何东西....我的绘画课如下...

public class Draw extends View {
String info = "";
float x = 0;
float y = 0;
int color = Color.GREEN;
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Draw(Context context, AttributeSet attrs) {
super(context, attrs);

}

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

Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setColor(color);
paint.setStrokeWidth(15);
paint.setTextSize(30);

canvas.drawLine(x-10, y, x+10, y, paint);
canvas.drawLine(x, y-10, x, y+10, paint);
canvas.drawText(info, x, y, paint);

}

public void updateInfo(String t_info, float t_x, float t_y){
info = t_info;
x = t_x;
y = t_y;

invalidate();
}

public void clearInfo(){
info = "";
x = 0;
y = 0;
invalidate();
}
}

我从我的 mainactivity 类中调用它。但我首先从图像选择类中调用它,如下所示

public class FirstImage extends ImageView implements OnTouchListener {


MotionEvent event;
int a;
Bitmap image;
String huma ="human";
String info = "human";
float x = 0; //init value
float y = 0; //init value
Animation animationFadeIn;


public FirstImage(Context context) {
super(context);




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


public void changeImage(int id){

this.setImageResource(id);
a=id;
final Animation animationFadeout=AnimationUtils.loadAnimation(getContext(), R.anim.zoomin);
this.startAnimation(animationFadeout);
this.setOnTouchListener(this);

}
@Override
public boolean onTouch(View arg0, MotionEvent me) {

switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
case MotionEvent.ACTION_MOVE:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
case MotionEvent.ACTION_UP:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
case MotionEvent.ACTION_OUTSIDE:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
default: return true;
}
return false;
}
public void pageinfo(float x, float y) {
// TODO Auto-generated method stub
((AshActivity)getContext()).updateMsg(info, x,y);
}
}

它是从主要 Activity 类中调用的

 public class AshActivity extends Activity  {
ImageView i;
Draw j;
TextView t,k;
ImageButton back;
ImageButton next;
OnClickListener l2 = null;
OnClickListener l = null;
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
i = (FirstImage)findViewById(R.id.image);


j=(Draw)findViewById(R.id.info);
t=(TextView)findViewById(R.id.textView);
k=(TextView)findViewById(R.id.textView1);
back=(ImageButton)findViewById(R.id.button1);
next=(ImageButton)findViewById(R.id.button2);
if (count==0){
((FirstImage) i).changeImage(R.drawable.human);

}
//addListenerOnButton();
}


public void updateMsg(String info, float x, float y) {
// TODO Auto-generated method stub
j.updateInfo(info, x, y);
}



}

我的布局xml文件如下

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:name="http://schemas.android.com/apk/res/com.example.nam"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


<!-- Screen Design for VIDEOS -->
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:text="pic on click which will tell where is the dna located in human body or cell "
android:textSize="18dip" />
</TableRow>


<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.nam.FirstImage
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<com.example.nam.Draw
android:id="@+id/info"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />

</FrameLayout>
</TableRow>

<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageButton
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/monotone_arrow_next_left"/>
<ImageButton
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/monotone_arrow_next_lrightcopy"/>

</LinearLayout>

</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:text="please don't mind "
android:textSize="18dip" />
</TableRow>

</TableLayout>
</ScrollView>

有什么问题我没明白....

最佳答案

问题是你的Draw实例的layout_height是0。

这是由于您为其注册的LayoutParams (wrap_content);如果您更改 FrameLayout 的 属性,将显示所需的文本:

<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ro.rekaszeru.sample.FirstImage android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ro.rekaszeru.sample.Draw android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>

另请注意,30 的文本大小非常大...

关于android - ondraw 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696197/

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