设计模式之工厂模式
Instead of calling the constructor, you call a method that creates an object for you, then method returns the object.
Res(resource) 文件夹里面的的文件不能有重复的名字,即使他们是不一样的格式
TOAST
Pop up message that disappear on its own
See codes for how to make toast
MainActivity.java
package edu.byuh.cis.cs203.hellocs203;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LetiView lv = new LetiView(this);
setContentView(lv);
}
}
LetiView.java
package edu.byuh.cis.cs203.hellocs203;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.widget.Toast;
public class LetiView extends View {
private Bitmap duck;
private boolean init;
public LetiView(Context c) {
super(c);
init = false;
duck = BitmapFactory.decodeResource(getResources(), R.drawable.duck);
Toast t = Toast.makeText(c, "This is another factory example!", Toast.LENGTH_LONG);
t.show();
}
@Override
public void onDraw(Canvas c) {
float w = c.getWidth();
float h = c.getHeight();
if (init == false) {
float duckSize = w * 0.2f;
duck = Bitmap.createScaledBitmap(duck,
(int)duckSize, (int)duckSize, true);
init = true;
}
c.drawColor(Color.GREEN);
float duckPosX = w/2 - duck.getWidth()/2;
float duckPosY = h/2 - duck.getHeight()/2;
c.drawBitmap(duck, duckPosX, duckPosY, null);
}
}