2017-08-11 1 views
1

ユーザーがボタンを使用して独自のパスを構築できるアプリケーションを作成したいと考えています。私はXとYの座標を更新することに問題がありますが、基本的にどのように私はそれを作ることができないのか分かりません。私の座標はまだ更新されていないので、これまでに行ったことは同じ点で描いています。私はgetメソッドとsetメソッドでいくつかのテストを行いましたが、私の考えは正しくないので、エラーを避けるためにコードに入れませんでした。私が達成したいもの:2つのポイントがあります - 開始ポイントと終了ポイント、ボタンを押すとキャンバスにラインが描画され、別のボタンを押すと別のラインが描画されますが最初のものと同じ座標には描画されませんしかし、たとえばなどボタンをクリックしたときのX座標とY座標の更新

を以前の1の終点で:私は再び一番上のボタンを押したときに

canvas.drawLine(300,250,350, 250,paint); 

:私は右のボタンを押したときに

canvas.drawLine(300,300,300, 250,paint); 

:私は一番上のボタンを押すと :

canvas.drawLine(350,250,350, 200,paint); 

1つの行の終了点は、別の1つの開始点である必要があります。ボタンをクリックすると、方向によってXまたはY座標に値50が加算または減算されます。

私はそれが今どのように動作し、どのようにしたいのかを示す画面を追加しています。私は助けやヒントを探しています。

ボタンの左右を間違った場所にあるが、それはここでは重要ではありません

public class MainActivity extends ActionBarActivity { 

Button top; 
Button bottom; 
Button left; 
Button right; 
Button clear; 
ImageView img; 

int x=300; 
int y=300; 

int x1=300; 
int y1=300; 

Integer gettingX() { 
    return x; 
} 

void settingX(Integer x) { 
    this.x = x; 
} 

Integer gettingY() { 
    return y; 
} 

void settingY(Integer y) { 
    this.y = y; 
} 


Integer gettingY1() { 
    return y1; 
} 

void settingY1(Integer y1) { 
    this.y1 = y1; 
} 

Integer gettingX1() { 
    return x1; 
} 

void settingX1(Integer x1) { 
    this.x1 = x1; 
} 
Paint paint = new Paint(); 
Bitmap bmp = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bmp); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    img = (ImageView) findViewById(R.id.img); 
    top = (Button) findViewById(R.id.top); 
    bottom = (Button) findViewById(R.id.bottom); 
    left = (Button) findViewById(R.id.left); 
    right = (Button) findViewById(R.id.right); 
    clear = (Button) findViewById(R.id.clear); 


    top.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      paint.setAntiAlias(true); 

      paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(25); 
      canvas.drawLine(x,y,x1, y1-50,paint); 
      img.setImageBitmap(bmp); 
     } 
    }); 

    bottom.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      paint.setAntiAlias(true); 

      paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(25); 
      canvas.drawLine(x,y,x1, y1+50,paint); 
      img.setImageBitmap(bmp); 
     } 
    }); 

    left.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      paint.setAntiAlias(true); 

      paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(25); 
      canvas.drawLine(x,y,x1-50, y1,paint); 
      img.setImageBitmap(bmp); 
     } 
    }); 

    right.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      paint.setAntiAlias(true); 

      paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(25); 
      canvas.drawLine(x,y,x1+50, y1,paint); 
      img.setImageBitmap(bmp); 
     } 
    }); 

    clear.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      canvas.drawColor(Color.WHITE); 
      img.setImageBitmap(bmp); 
     } 
    }); 

} 
} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res     
/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/top" 
android:text="top" 
android:layout_gravity="center_horizontal" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="right" 
    android:id="@+id/right" 
    android:layout_gravity="right" 
    android:layout_alignTop="@+id/left" 
    android:layout_toLeftOf="@+id/bottom" 
    android:layout_toStartOf="@+id/bottom" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="left" 
    android:id="@+id/left" 
    android:layout_below="@+id/top" 
    android:layout_toRightOf="@+id/top" 
    android:layout_toEndOf="@+id/top" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="bottom" 
    android:id="@+id/bottom" 
    android:layout_gravity="center_horizontal" 
    android:layout_below="@+id/right" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Clear" 
    android:id="@+id/clear" 
    android:layout_gravity="center_horizontal" 
    android:layout_below="@+id/bottom" 
    android:layout_alignRight="@+id/bottom" 
    android:layout_alignEnd="@+id/bottom" /> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:id="@+id/img" 
    android:layout_gravity="center_horizontal" 
    android:layout_below="@+id/clear" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

</RelativeLayout> 

current solution expected solution

答えて

1

問題は、あなたのコードは決してアップデートということですボタンをクリックすると、xまたはyの値が表示されます。ただxyを使用するx1またはy1にあなたの参照のすべてを更新し、

Integer gettingX() { ... } 
void settingX(Integer x) { ... } 
Integer gettingY() { ... } 
void settingY(Integer y) { ... } 
Integer gettingY1() { ... } 
void settingY1(Integer y1) { ... } 
Integer gettingX1() { ... } 
void settingX1(Integer x1) { ... } 

次へ:

int x1=300; 
int y1=300; 

これらの方法のすべてを削除します。

まず、これら2つの変数を削除します。最後に

  canvas.drawLine(x, y, x, y - 50, paint); 

、ラインを描画した後xまたはyに「新しい」の値を保存します:たとえば

  y -= 50; 

ここにあなたの「上」ボタンのクリックリスナーのために更新されたコードです

top.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      paint.setAntiAlias(true); 
      paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(25); 

      canvas.drawLine(x, y, x, y - 50, paint); 
      img.setImageBitmap(bmp); 

      y -= 50; 
     } 
    }); 
+0

ありがとう、問題解決済み:) – AidanSalvatore

関連する問題