android程式設計實例入門 -Sample改寫分享 (2014/10/06)

android程式設計實例入門 -Sample改寫分享 (2014/10/06)

android程式設計實例入門 -Sample改寫分享 (2014/10/06)

 

此範例為程式碼(05\Sample03)的改寫,利用一個LinearLayout、一個TextView、三個RadioButton和一個ImageView實作GUI界面,並且實作BitmapFactory載入圖片到Bitmap並利用ImageView顯示外加利用RadioButton和Matrix控制圖片縮放使用範例,程式碼如下所示:

 

 

package com.jashsample;
import java.text.SimpleDateFormat;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/*
 * BitmapFactory載入圖片到Bitmap並利用ImageView顯示外加控制縮放圖片旋轉使用範例。
 */
public class MainActivity extends Activity {
TextView tv;//為了之前的事件程式
ImageView iv;
RadioButton rb1, rb2, rb3;
RadioGroup rg;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);
tv = new TextView(this);//為了之前的事件程式
ll.addView(tv);//為了之前的事件程式
rb1 = new RadioButton(this);
rb2 = new RadioButton(this);
rb3 = new RadioButton(this);
rb1.setText("普通");
rb2.setText("放大");
rb3.setText("縮小");
RadioGroup rg = new RadioGroup(this);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
rb1.setChecked(true);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.car);
iv = new ImageView(this);
iv.setImageBitmap(bmp);
ll.addView(rg);
ll.addView(iv);
rb1.setChecked(false);//預設不要選擇
rb2.setChecked(false);
rb3.setChecked(false);
rb1.setOnClickListener(new SampleClickListener());
rb2.setOnClickListener(new SampleClickListener());
rb3.setOnClickListener(new SampleClickListener());
}
public boolean onCreateOptionsMenu1(Menu menu)
{
menu.add(Menu.NONE, 0, 0, "汽車");
menu.add(Menu.NONE, 1, 1, "計程車");
menu.add(Menu.NONE, 2, 2, "戰車");
menu.add(Menu.NONE, 3, 3, "敞篷車");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem mi)
{
String str = null;
switch(mi.getItemId())
{
case 0:
str = "汽車";
break;
case 1:
str = "計程車";
break;
case 2:
str = "戰車";
break;
case 3:
str = "敞篷車";
break;
}
tv.setText("��" + str + "對吧。");
return true;
}
/*
    public boolean onKey(View v, int keyCode, KeyEvent event)//當TextView有駐點時事件無效
    {
        tv.setText(et.getText());
        return false;
    }
    */
class SampleItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?>v, View iv, int pos, long id)
{
TextView tmp = (TextView) iv;
tv.setText("��" + tmp.getText() + "對吧。");
}
public void onNothingSelected(AdapterView<?> arg0){}
}
class SampleCheckedChangeListener implements OnCheckedChangeListener
{
public void onCheckedChanged(CompoundButton cb, boolean isChecked)
{
if(isChecked == true)
{
tv.setText("我要買" + cb.getText() + "。");
}
else if(isChecked == false)
{
tv.setText("我不買" + cb.getText() + "了。");
}
}
}
public boolean onTouchEvent(MotionEvent e)//直接在Activity上實作Touch事件
{
if(e.getAction() == MotionEvent.ACTION_DOWN)//設定在畫面任何地方Touch都改變tv文字
{
tv.setText("您好再見");
}
else if(e.getAction() == MotionEvent.ACTION_UP)
{
tv.setText("再見您好");
}
return true;
}
class SampleTouchListener implements OnTouchListener
{
public boolean onTouch(View v, MotionEvent e)
{
if(e.getAction() == MotionEvent.ACTION_DOWN)
{
tv.setText("您好");
}
else if(e.getAction() == MotionEvent.ACTION_UP)
{
tv.setText("再見");
}
return true;
}
}
class SampleItemClickListener implements OnItemClickListener
{
public void onItemClick(AdapterView<?>v, View iv, int pos, long id)
{
TextView tmp = (TextView) iv;
tv.setText("��" + tmp.getText() + "對吧。");
}
}
class SampleClickListener implements OnClickListener//按鈕事件反��類別
{
public void onClick(View v)
{
Matrix m = new Matrix();
rb1.setChecked(false);//預設不要選擇
rb2.setChecked(false);
rb3.setChecked(false);
if(v == rb1)
{
m.postScale(1, 1);
rb1.setChecked(true);
}
else if(v == rb2)
{
m.postScale(2, 2);
rb2.setChecked(true);
}
else if(v == rb3)
{
m.postScale(0.5F, 0.5F);
rb3.setChecked(true);
}
Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
iv.setImageBitmap(bmp2);
}
}
}

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *