1

这是我的 mainactivity 代码:ResizeLineActivity.java 在其中我可以增加 lineincr 按钮单击时的线宽,而 linedecr 按钮单击时线宽正在减小,但问题是减小的线宽仅显示在 lineincr 按钮单击上。我不明白代码中有什么问题。驾驶室有人解释或更正此代码以在 linedecr 按钮单击时显示减少的行吗?

package edu.interview;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ResizeLineActivity extends Activity {
DrawLine drawLine;
Button Lineincr;
Button linedecr;
LinearLayout.LayoutParams layoutParams;
int i=0;
static float x1=50;
static float y1=100;
static float x2=90;
static float y2=100;
static int temp=0;
@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Lineincr =(Button)(findViewById(R.id.btn_incr));
    linedecr= (Button)findViewById(R.id.btn_decr);
    final LinearLayout linearltdrawLine=(LinearLayout)findViewById(R.id.linearlayout_showLineView);
//  linearltdrawLine.addView(new DrawLine(this, 50, 100, 100, 100));
    Lineincr.setOnClickListener(new View.OnClickListener() {
    LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        @Override
        public void onClick(View v) {
        //  temp=++i;
            // TODO Auto-generated method stub
            linearltdrawLine.removeAllViews();
            Toast.makeText(getApplicationContext(), "hello Line", Toast.LENGTH_SHORT).show();
            linearltdrawLine.addView(new DrawLine(getApplicationContext(), x1, y1, x2, y2));
            int width1=linearltdrawLine.getWidth();
            /*  TextView txtv=(TextView) findViewById(R.id.linearlayout_showLineView);
            txtv.setBackgroundColor(Color.CYAN);
            txtv.setText("Insteas of line");*/
            linearltdrawLine.invalidate();
            linearltdrawLine.refreshDrawableState();
        //  linearltdrawLine.addView(txtv,layoutParams);
            linearltdrawLine.setVisibility(View.GONE);
            //linearltdrawLine.setVisibility(View.VISIBLE);
        //  linearltdrawLine.invalidate();
        //  linearltdrawLine.removeView(new DrawLine(getApplicationContext(), x1, y1, x2, y2));
            for(i=0;i<=1;++i){
                x2=x2+1;
                linearltdrawLine.setVisibility(View.VISIBLE);
                linearltdrawLine.addView(new DrawLine(getApplicationContext(), x1, y1, x2, y2));
            //  linearltdrawLine.refreshDrawableState();

            }
        }
    });


    linedecr.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v1) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Line is decreasing", Toast.LENGTH_SHORT).show();
            linearltdrawLine.addView(new DrawLine(getApplicationContext(), x1, y1, x2, y2));
            linearltdrawLine.invalidate();

            linearltdrawLine.refreshDrawableState();
            //linearltdrawLine.setVisibility(View.GONE);
            for(i=1;i>0;i--){
                linearltdrawLine.removeView(new DrawLine(getApplicationContext(),x1,y1,x2,y2));
                x2=x2-1;
                linearltdrawLine.invalidate();
                linearltdrawLine.addView(new DrawLine(getApplicationContext(), x1, y1, x2, y2));
                linearltdrawLine.setVisibility(View.VISIBLE);
            }
        }
    });
}

}

画线.java

package edu.interview;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.graphics.Color;

public class DrawLine extends View{
private final float X1,Y1,X2,Y2;
private final Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
public DrawLine(Context context,float x1,float y1,float x2, float y2) {
    // TODO Auto-generated consructor stub
    super(context);
    this.X1=x1;
    this.Y1=y1;
    this.X2=x2;
    this.Y2=y2;

}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    paint.setTextSize(20);
    paint.setColor(Color.RED);

    canvas.drawLine(X1, Y1, X2, Y2, paint);
}

}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="#BEE40F" >
<TextView android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linearlayout_showLineView" android:orientation="vertical" android:layout_weight="0.81" android:weightSum="1">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linearLayout_showtxt" android:orientation="vertical" android:layout_weight="0.26"></LinearLayout>
</LinearLayout>
<RelativeLayout android:id="@+id/relativeLayout1" android:background="@android:color/white" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_weight="0.06">
    <Button android:layout_height="wrap_content" android:id="@+id/btn_incr"
         android:layout_width="wrap_content" android:text="Line++"
          android:layout_alignParentTop="true" android:layout_alignParentRight="true"
         android:layout_marginRight="106dp" android:layout_alignParentBottom="true" 
         >
    </Button>
    <Button android:layout_height="wrap_content" android:id="@+id/btn_decr"
             android:layout_width="wrap_content" android:text="Line --" 
             android:layout_toLeftOf="@+id/btn_decr" android:layout_marginRight="19dp"
              android:layout_alignParentBottom="true" android:layout_alignParentTop="true">
    </Button>
</RelativeLayout>
</LinearLayout>
4

0 回答 0