я работаю над приложением, в котором мой метод ondraw не работает должным образом...... в моем приложении я использую framelayout... поэтому я использую три класса... 1) acticvity 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.but, я сначала вызываю его из класса выбора изображения, как следует
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);
}
}
он вызывается из основного класса activitry
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
(wrap_content
); Если вы измените FrameLayout's
, отобразится нужный текст:
<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 очень велик...