2019.11.22 Composite Design Pattern 写一个相册浏览器

看起来像这样,结构如上图
MainActivity.java

package edu.byuh.cis.cs203.photobrowser;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    private int currentImageIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout.LayoutParams nice = new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT, 1);

        final String[] roomNames = {
                "The Bank",
                "The Gold Room",
                "The Green Room",
                "The Machine Room",
                "The Milk Room",
                "The Office",
                "The Pink Room",
                "The Warm Room",
                "The White Room"
        };

        final int[] imageIDs = {
                R.drawable.bank,
                R.drawable.gold_room,
                R.drawable.green_room,
                R.drawable.machine_room,
                R.drawable.milk_room,
                R.drawable.office,
                R.drawable.pink_room,
                R.drawable.warm_room,
                R.drawable.white_room
        };

        currentImageIndex = 0;

        TextView tv = new TextView(this);
        tv.setText(roomNames[currentImageIndex]);
        tv.setTextSize(50);
        tv.setGravity(Gravity.CENTER);
        tv.setLayoutParams(nice);

        ImageView iv = new ImageView(this);
        iv.setImageResource(imageIDs[currentImageIndex]);
        iv.setLayoutParams(nice);

        Button left = new Button(this);
        left.setText("<");
        left.setLayoutParams(nice);
        left.setOnClickListener(v -> {
            currentImageIndex = currentImageIndex - 1;
            if (currentImageIndex < 0) {
                currentImageIndex = imageIDs.length-1;
            }
            iv.setImageResource(imageIDs[currentImageIndex]);
            tv.setText(roomNames[currentImageIndex]);
        });

        Button right = new Button(this);
        right.setText(">");
        right.setLayoutParams(nice);
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentImageIndex = (currentImageIndex+1) % imageIDs.length;
                iv.setImageResource(imageIDs[currentImageIndex]);
                tv.setText(roomNames[currentImageIndex]);
            }
        });

        LinearLayout lnl = new LinearLayout(this);
        lnl.setOrientation(LinearLayout.VERTICAL);
        LinearLayout buttons = new LinearLayout(this);
        buttons.setOrientation(LinearLayout.HORIZONTAL);
        lnl.addView(tv);
        lnl.addView(iv);
        buttons.addView(left);
        buttons.addView(right);
        lnl.addView(buttons);

        setContentView(lnl);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值