public class Harshida extends View {
Bitmap gBall;
float changingY;
Typeface font;
public Harshida(Context context) {
super(context);
// TODO Auto-generated constructor stub
gBall=BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
changingY=0;
font=Typeface.createFromAsset(context.getAssets(), "assets/G-Unit.TTF");
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint textPaint=new Paint();
textPaint.setARGB(50,254,10,50);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(50);
textPaint.setTypeface(font);
canvas.drawText("HarshidaParmar",canvas.getWidth()/2,200,textPaint);
//canvas.drawBitmap(gBall,(canvas.getWidth()/2),0,null);
canvas.drawBitmap(gBall,(canvas.getWidth()/2),changingY,null);
if(changingY < canvas.getHeight()){
changingY +=10;
}else {
changingY=0;
}
Rect middleRect= new Rect();
middleRect.set(0, 40, canvas.getWidth(),400);
//middleRect.set(0,0,0,0);
Paint ourBlue = new Paint();
ourBlue.setColor(Color.BLUE);
canvas.drawRect(middleRect, ourBlue);
//canvas.drawRect(middleRect,ourBlue);
invalidate();
}
}
可能重复:http://stackoverflow.com/questions/3203694/custom-fonts-in-android/3203724#3203724 – Praveen 2010-09-15 11:19:19
@Paresh:任何资源(R.id.foo) – 2010-09-15 11:56:38
@Praveen:我猜测它的代码相同。但我仍然得到这个运行时异常 – 2010-09-15 11:59:53