2013-01-04 4 views
1

위치를 변경하려면 화면의 사각형 중 하나를 누를 때마다 싶습니다. 또는 전체 SurfaceView를 새로 고칩니다.새로 고침 SurfaceView -> Canvas onClickListener 안드로이드

앱이 실행되는 것 같지만 사각형이 카운터 위치를 변경하지 않습니다.

대단히 감사합니다.

다음
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<SurfaceView 
    android:id="@+id/surface" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/hello_world" 
    tools:context=".MainActivity" /> 
<Button 
    android:id="@+id/l" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="16dp" 
    android:text="L" /> 
<Button 
    android:id="@+id/r" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="19dp" 
    android:text="R" /> 
</RelativeLayout> 

자바입니다 : 각 위치 변경 후 새로 고침 서피스 뷰 SurfaceView에 무효화 메소드를 호출하는

package com.example.rectangle; 

import android.app.Activity; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.view.SurfaceHolder; 
import android.view.SurfaceHolder.Callback; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
DrawView drawView; 
Paint paint = new Paint(); 
int w, h, edge, axis_w; 
int counter; 
Button add, sub; 
TextView display; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    counter = 0; 
    add = (Button) findViewById(R.id.r); 
    sub = (Button) findViewById(R.id.l); 
    display = (TextView) findViewById(R.id.textView1); 

    add.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter += 10; 
      display.setText("Total is" + counter); 

     } 
    }); 

    sub.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter -= 10; 
      display.setText("Total is" + counter); 
     } 
    }); 

    SurfaceView surface = (SurfaceView) findViewById(R.id.surface); 

    surface.getHolder().addCallback(new Callback() { 

     public void surfaceCreated(SurfaceHolder holder) { 

      Canvas canvas = holder.lockCanvas(); 

      w = canvas.getWidth(); 
      h = canvas.getHeight(); 
      edge = 5; 
      axis_w = 3; 
      // canvas.drawRect(left, top, right, bottom, paint) 
      // Background 
      paint.setColor(Color.rgb(255, 255, 255)); 
      canvas.drawRect(0, 0, w, h, paint); 
      // Canvas Background Color 
      paint.setColor(Color.rgb(0, 206, 209)); 
      // Borders 
      canvas.drawRect(0, 0, w, edge, paint); 
      canvas.drawRect(0, h - edge, w, h, paint); 
      canvas.drawRect(0, 0, edge, h, paint); 
      canvas.drawRect(w - edge, 0, w, h, paint); 
      // Axis 
      canvas.drawRect(((w - axis_w)/2) + counter, 0, 
        ((w + axis_w)/2) + counter, h, paint); 
      canvas.drawRect(0, (h - axis_w)/2, w, (h + axis_w)/2, paint); 

      holder.unlockCanvasAndPost(canvas); 
     } 

     public void surfaceDestroyed(SurfaceHolder holder) { 
     } 

     public void surfaceChanged(SurfaceHolder holder, int format, 
       int width, int height) { 
     } 
    }); 
} 
} 

답변

2

확인. 다른 해결책을 시도하십시오. 사각형을 그릴 뷰 클래스를 직접 만드십시오. 몇 가지 코드를 준비했습니다. 필요에 맞게 사용자 정의하십시오. 버튼을 클릭하면

Simlpe의 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/mainL" 
    > 
<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, MainActivity" 
    android:id="@+id/button" 
    /> 
</LinearLayout> 

및 자바 코드

public class MainActivity extends Activity 
{ 
    MyRectg myRectg; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     myRectg = new MyRectg(this); 
     ((LinearLayout)findViewById(R.id.mainL)).addView(myRectg); 
     ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() { 

     public void onClick(View arg0) 
     { 
      myRectg.invalidate(); 
     } 
    }); 
} 

public class MyRectg extends View 
{ 
    public MyRectg(Context c) 
    { 
     super(c); 
    } 

    @Override 
    public void onDraw(Canvas c) 
    { 
     //this simulate new positions calculating 
     Random rnd = new Random(); 
     Paint p = new Paint(); 
     p.setColor(Color.GREEN); 
     c.drawRect(rnd.nextFloat()*100, rnd.nextFloat()*500, rnd.nextFloat()*300, rnd.nextFloat()*500, p); 
    } 
} 

지금 캔버스 무작위로 이동합니다. 이게 니가 원하는거야?

+0

답변 해 주셔서 대단히 감사합니다. 아주 잘 작동하지만, 약간 다른 것을하고 싶었지만 메인 직사각형 좌표로 다시 계산한다는 생각은 같습니다. – Gabi

+0

불행히도 내 평판이 낮아서 대답을 투표 할 수 없습니다. – Gabi

+0

나는 내가하고 싶은 일에 따라 코드를 수정했으며 매우 잘 작동합니다. 다시 한 번 감사드립니다. – Gabi

0

시도

토끼는 XML 코드입니다.

EDIT :

그리고 surfaceChanged 방법에 surfaceCreated에서 위치 계산을 이동.

+0

감사하지만 Invalidate 메소드는 캔버스 그리기에서만 작동합니다. 나는 여전히 두 번째 혼잡에 대해 노력하고있다. – Gabi

+0

캔버스를 표면에 넣었다.하지만 작동하지 않는 것 같아. – Gabi

관련 문제