Thiết kế Android game: Touch Gallery Images

Thiết kế Android game: Touch Gallery Images

Mục tiêu:

Xây dựng ứng dụng hiển thị Slide show một Gallery đơn giản sau một khoảng thời gian định trước sẽ tuần tự hiện các hình ảnh trong danh sách

Cho phép touch vào màn hình để chuyển đổi xem ảnh khác.

Chuẩn bị: Chép 4 file hình ảnh sample_0.jpg, sample_1.jpg, sample_2.jpg, sample_2.jpg vào folder res/drawable-hdpi

Bước 1: Thiết kế giao diện

Kéo ImageView từ cột Palette sang màn hình thiết bị. Các bạn thiết kế như hình sau:

Thiết kế Android game: Touch Gallery Images

Bước 2:

Viết code xử lý của file MainActivity.java trong folder src

Viết code khai báo 1 mảng kiểu int chứa danh sách tham chiếu hình ảnh mỹ nhân, 1 mảng String chứa danh sách họ tên mỹ nhân

int img_list[] ={
            R.drawable.sample_0,
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3
    };
    
String img_description_list[]={
            "Điêu Thuyền",
            "Đắc Kỷ",
            "Tây Thi",
            "Dương Quý Phi"
    }; 

Viết code cho sự kiện người dùng touch trên màn hình thì chuyển ảnh

LinearLayout layout = (LinearLayout) findViewById(R.id.root);
        
        layout.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                tv.setText(img_description_list[count]);
                img.setImageResource(img_list[count]);

                count++;
                if(count==4) count=0;                
            }
        }); 

Viết code định thời gian tuần tự chuyển đổi ảnh.

new CountDownTimer(15000, 3000) {
            
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub

                tv.setText(img_description_list[count]);
                img.setImageResource(img_list[count]);

                count++;
                if(count==4) count=0;
            }
            
            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                tv.setText("The End. Thanks you!!!");
            }
        }.start(); 
Bạn thấy bài viết này như thế nào?: 
Average: 10 (1 vote)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Multi-headed trong Drupal

Multi-headed trong Drupal

One of the strengths of Drupal is that it's not just a tool for building a single website. It can be used equally well for managing multiple companion sites together.

10 điều người dùng muốn ở iPad 3

10 điều người dùng muốn ở iPad 3

Đó sẽ phải là những tính năng “lấp lánh”, đủ sức đánh bật mọi thiết bị trước nay đã tồn tại trên thị trường máy tính bảng, kể cả người anh Apple iPad 2.

HTC EVO 4G Review, A Must Buy Phone on Android

HTC EVO 4G Review, A Must Buy Phone on Android

HTC EVO 4G comes with all the features which are beyond the imaginations of its user.

Công ty diệt chuột T&C

 

Diet con trung