2019.9.16

设计模式之工厂模式
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);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值