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

 
Hướng dẫn gửi HTML Emails từ Drupal Webform module

Hướng dẫn gửi HTML Emails từ Drupal Webform module

The Webform module is one of the most popular modules in Drupal.

Nên sử dụng Existing Drupal Fields để tạo Fields mới

Nên sử dụng Existing Drupal Fields để tạo Fields mới

Drupal gives you the ability to re-use fields. If you have an "Image" field, you could choose to use that same field on every content type on your site.

Máy tính bảng Courier

Bí mật đằng sau vụ Microsoft “khai tử” máy tính bảng Courier

Dù đã bị khai tử hồi tháng 5/2010 nhưng câu chuyện đằng sau “cái chết” bất ngờ và đầy nghi vấn của máy tính bảng Courier vẫn thu hút sự quan tâm của giới công nghệ

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

 

Diet con trung