1. 程式人生 > >Android應用程式開發期末大作業(2)

Android應用程式開發期末大作業(2)

(3)用imageView和imagebutton及activity在介面上實現圖片瀏覽

新建android專案如AI03,在專案的/AI03/src/com/example/ai03/MainActivity.java檔案寫下如下程式碼,注意包名和圖片存放的位置!

圖片存放目錄位置為/AI03/res/drawable-hdpi


package com.example.ai03;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	//宣告Image物件與ImageBoutton物件  
    private ImageView ivwPicture=null;  
    private ImageButton ibtnProv=null;  
    private ImageButton ibtnNext=null;  
    //宣告5個影象  
    private Integer[] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        //獲取ImageView物件ivwPicture  
        ivwPicture=(ImageView)super.findViewById(R.id.picture);  
        //獲取兩個按鈕物件ImageButton  
        ibtnProv=(ImageButton)super.findViewById(R.id.prov);  
        ibtnNext=(ImageButton)super.findViewById(R.id.next);  
        //註冊OnClick監聽器  
        ibtnProv.setOnClickListener(new ProvOnClickListener());  
        ibtnNext.setOnClickListener(new NextOnClickListener());  
        //註冊OnlongClick監聽器  
        ivwPicture.setOnLongClickListener(new PicOnLongClick());  
    }  
    //單擊“上一幅”按鈕顯示前一張圖片  
    private class ProvOnClickListener  implements OnClickListener{  
        private int i=5;  
        public void onClick(View view){           
            if(i > 0){  
                ivwPicture.setImageResource(iImages[--i]);  
            }  
            else if(i == 0){  
                i =5;  
                ivwPicture.setImageResource(iImages[4]);  
            }  
        }  
    }  
    //單擊“下一幅”按鈕顯示後一張圖片  
    private class NextOnClickListener implements OnClickListener{  
        private int i=0;  
        public void onClick(View view){           
            if(i < 5)  
                ivwPicture.setImageResource(iImages[i++]);  
            else if(i == 5){  
                i = 0;  
                ivwPicture.setImageResource(iImages[0]);  
            }  
        }  
    }  
    //長按圖片設定為桌面牆紙  
    private class PicOnLongClick implements OnLongClickListener{  
        @Override  
        public boolean onLongClick(View view){  
            try{  
                //清空當前牆紙  
                MainActivity.this.clearWallpaper();  
                //當前view轉換為ImageView物件  
                ImageView iv=(ImageView)view;  
                //啟用圖形緩衝  
                iv.setDrawingCacheEnabled(true);  
                //使用當前緩衝圖形建立Bitmap  
                Bitmap bmp=Bitmap.createBitmap(iv.getDrawingCache());  
                //當前圖形設定為牆紙  
                MainActivity.this.setWallpaper(bmp);  
                //清理圖形緩衝  
                iv.setDrawingCacheEnabled(false);  
                Toast.makeText(getApplicationContext(), "背景設定成功!",Toast.LENGTH_LONG).show();  
            }  
            catch(Exception e){  
                Toast.makeText(getApplicationContext(), "背景設定失敗!",Toast.LENGTH_LONG).show();  
            }  
            return true;  
        }  
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
在專案的/AI03/res/layout/activity_main.xml檔案寫下如下程式碼。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ai03.MainActivity" >

    <ImageView  
        android:id="@+id/picture"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="0dp"  
        android:src="@drawable/a"  
        tools:ignore="ContentDescription" />

    <ImageButton
        android:id="@+id/prov"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/prov" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/picture"
        android:src="@drawable/next" />

</RelativeLayout>
在專案的/AI03/AndroidManifest.xml檔案新增如下程式碼。
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ai03"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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>
執行效果如下。


轉自:http://blog.csdn.net/jianghuiquan/article/details/8348680

(4)利用RadioButton、CheckBox、Activity等實現如圖的功能及效果

新建android專案如AI04,在專案的/AI01/src/com/example/ai04/MainActivity.java檔案寫下如下程式碼,注意包名!

package com.example.ai04;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	RadioGroup rg;
    TextView show;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//獲取介面上rg、show兩個元件
        rg=(RadioGroup)findViewById(R.id.rg);
        show=(TextView)findViewById(R.id.show);
        //為RadioGroup元件的OnCheck事件繫結事件監聽器
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                //根據使用者勾選的單選按鈕來動態改變tip字串的值
                String tip=checkedId==R.id.male?"您的性別是男人":"您的性別是女人";
                  //修改show元件中的文字
                show.setText(tip);
            }
            
        });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
在專案的/AI04/res/layout/activity_main.xml檔案寫下如下程式碼。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ai04.MainActivity" >

    <TableRow >
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性別:"
        android:textSize="16dp"/>
    <!-- 定義一組單選按鈕 -->
    <RadioGroup android:id="@+id/rg"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <!-- 定義兩個單選按鈕 -->
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/male"
            android:text="男"
            android:checked="true" />
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/female"
            android:text="女"/>
    </RadioGroup>
</TableRow>
   <TableRow >
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="喜歡的顏色:"
           android:textSize="16dp"/>
       <!-- 定義一個垂直的線性佈局 -->
       <LinearLayout
           android:layout_gravity="center_horizontal"
           android:orientation="vertical"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >
           <!-- 定義三個複選框 -->
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="紅色"
               android:checked="true" />
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="藍色"/>
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="綠色"/>
       </LinearLayout>
   </TableRow>
   <TextView android:id="@+id/show"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</TableLayout>
執行效果如下。


轉自:https://www.cnblogs.com/wolipengbo/p/3343443.html