1

我正在处理我的 ondraw 方法无法正常工作的应用程序......在我的应用程序中我正在使用框架布局......所以我使用了三个类...... 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);
    }
    } 

它是从主要活动类中调用的

 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>

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

4

1 回答 1

2

问题是您的Draw实例layout_height为 0。

这是因为LayoutParams您已经注册了它 ( 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 的文本大小非常大......

于 2012-05-22T07:24:56.080 回答