gpt4 book ai didi

android - 我们如何让用户在 Android 的 Canvas 上练习字母表?

转载 作者:行者123 更新时间:2023-11-29 02:06:36 25 4
gpt4 key购买 nike

我正在开发一个我想要的应用程序,用户可以练习英文字母,点击“练习”按钮。我在这里得到了一个引用,但这段代码不起作用。我是 android 的新手。任何人都可以帮助我编写代码,我该怎么做。

    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);

}
}
}

最佳答案

最好是创建一个显示字母表的 Activity 在上面画一个透明的 Canvas ,让用户在 Canvas 上画画透过透明胶片看 Activity

关于android - 我们如何让用户在 Android 的 Canvas 上练习字母表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com