2012-06-29 3 views
3

그래서 활동을 확장하는이 클래스가 있습니다. 하지만 화면에 무언가를 그리기를 원하므로 캔버스를 만들어야합니다. 그러나 뷰를 확장 할 수는 없습니다. 어떻게해야합니까?활동에 대한보기를 생성하는 방법

내 활동에는 몇 가지 물건을 사용하는 onClick 메서드가 있지만 onClick 메서드를 호출해도 간단한 이미지를 그릴 수 있습니다.

감사합니다.

public class Stuff extends Activity implements OnClickListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
(...) 
} 

@Override 
public void onClick(View arg0) { 
(...) 
} 
+1

진지하게 질문 고마워요 내를 요청하기 전에 구글에서 검색을 시도 , 당신은 정말로 일부 안드로이드 튜토리얼을 따라야합니다. 시작하려면 여기를 클릭하십시오. http : //developer.android.com/training/index.html – Squonk

+1

모두에게 감사드립니다. – LucaSC

답변

5

1 단계 :

public class DrawView extends View { 
    public float currentX=40; 
    public float currentY=50; 

    public DrawView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onDraw(Canvas canvas) {   
     super.onDraw(canvas); 
     Paint paint=new Paint(); 
     paint.setColor(Color.RED); 
     canvas.drawCircle(currentX, currentY, 25, paint); 
    } 

} 

:로보기를 확장하여 클래스를 만듭니다 2 단계 : 콘텐츠 포함 활동 :

public class Stuff extends Activity implements OnClickListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
LinearLayout root=(LinearLayout) findViewById(R.id.root); 
(...) 
} 

@Override 
public void onClick(View arg0) { 
//DRAW YOUR VIEW ON BUTTON CLICK 
final DrawView drawView=new DrawView(this); 
drawView.setMinimumWidth(300); 
drawView.setMinimumHeight(500); 
drawView.currentX=200; 
drawView.currentY=200; 
drawView.invalidate(); 
root.addView(drawView); 
(...) 
} 

3 단계 :으로 귀하의 활동 main.xml에 마지막으로

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#99FFCC" 
    android:id="@+id/root"> 
</LinearLayout> 

당신이 단계 것을 통과 할 수없는 경우,

0

XML 파일에서 레이아웃보기를 가져 오시겠습니까? 보기를 그릴 수 있고, 코드를 호출하여보고, 클릭 할 때 응답 할 이미지를 설정할 수 있습니다. 당신의에서 onCreate 방법에서

, super.onCreate(savedInstanceState); 후 이미 활동에 그것을 구현하기 때문에이 setContentView(R.id.layoutname)

예를 들어, OnClickListener를에 관해서는

@Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

,이처럼 구현하기 위해 뷰를 설정할 수 있습니다 추가 .

당신은 전직에 대한 UR 활동 내에서 내부 클래스를 선언 할 수
1

이 코드를 참조

// set this after "setContentView(R.layout.main);" 
b1 = (Button)findViewById(R.id.main); 
b1.setOnClickListener(this); 
:

public class GraphicsTest extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(new GraphicsTestView(this)); 
    } 


    private static class GraphicsTestView extends View 
    { 
    private ShapeDrawable mDrawable = 
     new ShapeDrawable(); 
    /* Drawable's are objects which can be drawn on a Canvas 
     ShapeDrawable is used to draw primitive shapes such as: 
     ArcShape, OvalShape, PathShape, RectShape, RoundRectShape 
     A Canvas is the object provided by Android on which 
     a view tries to draw itself. In addition to ShapeDrawable, 
     there are other subclasses of Drawable like PictureDrawable, 
     RotateDrawable, ScaleDrawable, ClipDrawable,GradientDrawable, etc 
     Some of these we will see when we consider the XML approach to 
     graphics 
    */ 

    public GraphicsTestView (Context context) 
    { 
     super(context); 
     setFocusable(true); 
     this.mDrawable.getPaint().setColor(0xFFFF0000); 
      //argb where a is alpha (transparency) 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    /* the onDraw method is where a view draws itself 
      this is our first time overriding it. 
     */ 
    { 
     int x = 10; 
     int y = 10; 
     int width = 300; 
     int height = 50; 
     this.mDrawable.setBounds(x, y, x + width, y + height); 
     this.mDrawable.draw(canvas); 

     ArcShape arc = new ArcShape(45,90); //start angle, sweep angle 
     ShapeDrawable test = new ShapeDrawable(arc); 

     Paint p = test.getPaint(); 
     p.setColor(0xFF00FFFF); 

     p.setStyle(Paint.Style.STROKE); 

     test.setBounds(10, 70, 310, 370); 
      //Top-Left, Bottom Right of rectangle to draw into 
     test.draw(canvas); 

     } 
    } 
} 
관련 문제