2012-03-14 2 views
2

나는 "연습"버튼을 클릭하여 영어 알파벳을 연습 할 수있는 응용 프로그램을 개발 중입니다. 여기에 참조가 있지만이 코드는 작동하지 않습니다. 나는 안드로이드를 처음 사용합니다. 누구든지 코드를 사용하면 어떻게 할 수 있습니까?안드로이드에서 캔버스에 알파벳을 연습 할 수있는 방법은?

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Paint.FontMetricsInt; 
import android.graphics.drawable.GradientDrawable; 
import android.graphics.drawable.GradientDrawable.Orientation; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

public class AlphaDraw extends View { 

    public interface AlphaScrollable { 
     public void scrollAlpha(char prefix); 
    } 

    public AlphaScrollable target; 

    public AlphaDraw(Context context) { 
     super(context); 
    } 

    public AlphaDraw(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public AlphaDraw(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    protected String[] alpha = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", 
      "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; 

    protected boolean touching = false; 

    // try capturing touches to jump to letters 
    // will actively adjust list location 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     float margin = this.getWidth()/4; 
     float fracy = (event.getY() - (margin * 2))/(this.getHeight() - (margin * 4)); 

     int approx = (int) (alpha.length * fracy); 
     // Log.d("ALPHA", String.format("approx=%s", alpha[approx])); 

     if (approx < 0 || approx >= alpha.length) 
     return true; 

     // jump to alpha location in list 

     // find first letter location in list 
     this.target.scrollAlpha(alpha[approx].charAt(0)); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
     this.touching = true; 
     this.invalidate(); 
     break; 

     case MotionEvent.ACTION_UP: 
     this.touching = false; 
     this.invalidate(); 
     break; 

     } 

     return true; 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     // show shadow only when being actively touched 

     float width = this.getWidth(), height = this.getHeight(); 
     float margin = this.getWidth()/4; 

     float spacing = (height - (margin * 4))/alpha.length; 

     int shadowColor = Color.argb(128, 0, 0, 0); 
     GradientDrawable shadow = new GradientDrawable(Orientation.LEFT_RIGHT, new int[] { shadowColor, shadowColor }); 
     shadow.setShape(GradientDrawable.RECTANGLE); 
     shadow.setCornerRadius(10); 

     shadow.setBounds((int) margin, (int) margin, (int) (width - margin), (int) (height - margin)); 

     if (touching) 
     shadow.draw(canvas); 

     // draw alphabet index 
     // remember that text is drawn from bottom 

     Paint paint = new Paint(); 
     paint.setColor(Color.argb(255, 255, 255, 255)); 
     paint.setFakeBoldText(true); 
     paint.setFlags(Paint.ANTI_ALIAS_FLAG); 

     // measure to figure out perfect text height 
     // W is a nice average letter 
     // read new metrics to get exact pixel dimensions 
     FontMetricsInt fm = paint.getFontMetricsInt(); 
     // float charHeight = Math.abs(fm.top) + Math.abs(fm.descent); 
     float startTop = (margin * 2) + Math.abs(fm.top); 

     for (int i = 0; i < alpha.length; i++) { 
     float charWidth = paint.measureText(alpha[i]); 
     float centered = (width - charWidth)/2; 

     canvas.drawText(alpha[i], centered, (i * spacing) + startTop, paint); 

     } 
    } 
} 
+0

을 통해 투명 필름을보고 그 캔버스 위에 그려 보자 그리고하는 것 당신이 직면하고있는 문제? – Deva

+0

@Deva :이 코드는 작동하지 않습니다. 이제 알파벳을 표시하고 투명 캔버스를 그릴 수있는 활동을 만들고 사용자가 그 캔버스 위에 그릴 수있는 활동을 투명 필름을 통해보고 싶습니다. 계속 진행할 수 있도록이 코드 나 참조 코드를 작성하는 것을 도와 주실 수 있습니까? 고맙습니다. – Delwin

답변

1

가장 좋은 것이 인 알파벳 가에 투명 캔버스를 그려 표시 할 것 활동을 생성하고 사용자가 활동

+0

도움이 필요하시면 알려주세요 –

+0

예. Aashish. 나도 같은 논리로 생각했지만 나는 안드로이드를 처음 사용하는 것처럼 진행하는 법을 모른다. 계속 진행할 수 있도록 코드 또는 참조로 나를 도울 수 있습니까? – Delwin

+0

Alphabets를 표시하는 활동을 만들 수 있습니까 ??? 캔버스로 도울 것입니다 –

관련 문제