Lập trình Android: Tạo hình nền (WallPaper) pptx

5 1.6K 7
Lập trình Android: Tạo hình nền (WallPaper) pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Tạo hình nền tự chọn WallPaper Hẳn các bạn đã quen với việc chỉnh hình nền cho điện thoại bằng công cụ của Smartphone. Đã bao giờ bạn tự hỏi ứng dụng đó hoạt động thế nào chưa ?? Sau đây mình sẽ tạo 1 ứng dụng tương tự đẻ miêu tả cách thức hoạt động của nó. 1/ Tạo 1 project như sau: SetWallpaper. Enter following information: Project name: SetWallpaper Build Target: Android 2.3 Application name: SetWallpaper Package name: com.example.SetWallpaper Create Activity: SetWallpaper 2/ Trước khi viết code bạn phải chỉnh lại file cấu hình AndroidManifest.xml để thêm vào quyền thay đổi hình nền: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.SetWallpaper" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.SET_WALLPAPER"> </uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SetWallpaper" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 3/ Sau đó bạn tạo giao diện ứng dụng trong file main.xml như sau: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN android:weightSum="1"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/set" android:layout_height="wrap_content" android:text="Chuyen thanh Wallpaper" android:layout_width="match_parent"></Button> <ImageView android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/preview" android:layout_weight="0.71" android:layout_width="match_parent"></ImageView> </LinearLayout> 4/ Tiếp theo bạn phải tạo 1 folder drawable nằm trong folder res và thêm 1 hình ảnh vào drawable. Đổi tên hình lại thành “five” (Lưu ý ko có .png) 5/ Bạn viết code trong SetWallpaper.java như sau: package com.example.SetWallpaper; import java.io.IOException; import android.app.Activity; import android.app.WallpaperManager; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN public class SetWallpaper extends Activity { Bitmap bitmap; int lastImageRef; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttonSetWallpaper = (Button)findViewById(R.id.set); ImageView imagePreview = (ImageView)findViewById(R.id.preview); imagePreview.setImageResource(R.drawable.five); buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); try { myWallpaperManager.setResource(R.drawable.five); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}); Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN } } Cuối cùng debug và đc kết quả như sau: Ban đầu: Sau khi bấm Button bạn sẽ đc Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN Bạn nào có ý kiến đóng góp vui lòng post bài vào forum trang web www.laptrinhdidong.vn . Mình sẽ theo dõi forum thường xuyên ^^ Lập trình Android – http://laptrinhdidong.vn Page 5 . Trung tâm Tin học – ĐH KHTN Tạo hình nền tự chọn WallPaper Hẳn các bạn đã quen với việc chỉnh hình nền cho điện thoại bằng công cụ của Smartphone. Đã bao giờ. xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" Lập trình Android – http://laptrinhdidong.vn. android:layout_width="match_parent"></Button> <ImageView android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/preview" android:layout_weight="0.71" android:layout_width="match_parent"></ImageView> </LinearLayout> 4/

Ngày đăng: 07/08/2014, 08:22

Tài liệu cùng người dùng

Tài liệu liên quan