|  | 
 
| 
专业相框设计 ImageView 的堆叠应用
 
 
 ①新建工程
 ②准备三张 png 图片
 
  ③修改 main.xml 布局,添加 UI 元素
 <?xml version="1.0" encoding="utf-8"?>
 
 <AbsoluteLayout
 
 android:id="@+id/widget34"
 
 android:layout_width="fill_parent"
 
 android:layout_height="fill_parent"
 
 xmlns:android="http://schemas.android.com/apk/res/android"
 
 ><!--创建第一个ImageView (第二层图片)-->
 
 <ImageView
 
 android:id="@+id/myImageView1"
 
 android:layout_width="wrap_content"
 
 android:layout_height="wrap_content"
 
 android:layout_x="0px"
 
 android:layout_y="36px"
 
 />
 
 <!--创建第二个ImageView (第一层图片)-->
 
 <ImageView
 
 android:id="@+id/myImageView2"
 
 android:layout_width="wrap_content"
 
 android:layout_height="wrap_content"
 
 android:layout_x="0px"
 
 android:layout_y="36px"
 
 />
 
 <!--创建第一个Button -->
 
 <Button
 
 
 
 android:id="@+id/myButton1"
 
 android:layout_width="105px"
 
 android:layout_height="66px"
 
 android:text="pic1"
 
 android:layout_x="9px"
 
 android:layout_y="356px"
 
 />
 
 <!--创建第二个Button -->
 
 <Button
 
 android:id="@+id/myButton2"
 
 android:layout_width="105px"
 
 android:layout_height="66px"
 
 android:text="pic2"
 
 android:layout_x="179px"
 
 android:layout_y="356px"
 
 />
 
 </AbsoluteLayout>
 复制代码
 ④修改 mainActivity.java
 package zyf.Ex_Ctrl_7;
 
 import android.app.Activity;
 
 import android.os.Bundle;
 
 import android.view.View;
 
 import android.widget.Button;
 
 import android.widget.ImageView;
 
 public class Ex_Ctrl_7 extends Activity {
 
 /** Called when the activity is first created. */
 
 /* 声明Button、ImageView 对象 */
 
 private ImageView mImageView01;
 
 private ImageView mImageView02;
 
 private Button mButton01;
 
 private Button mButton02;
 
 @Override
 
 public void onCreate(Bundle savedInstanceState) {
 
 super.onCreate(savedInstanceState);
 
 setContentView(R.layout.main);
 
 /* 取得Button、ImageView 对象 */
 
 mImageView01 = (ImageView) findViewById(R.id.myImageView1);
 
 mImageView02 = (ImageView) findViewById(R.id.myImageView2);
 
 mButton01 = (Button) findViewById(R.id.myButton1);
 
 mButton02 = (Button) findViewById(R.id.myButton2);
 
 /* 设置ImageView 背景图 */
 
 mImageView01.setImageDrawable(getResources().getDrawable(
 
 R.drawable.right));
 
 
 
 mImageView02.setImageDrawable(getResources().getDrawable(
 
 R.drawable.photo));
 
 /* 用OnClickListener 事件来启动 */
 
 mButton01.setOnClickListener(new Button.OnClickListener() {
 
 @Override
 
 public void onClick(View v) {
 
 /* 当启动后, ImageView 立刻换背景图 */
 
 mImageView01.setImageDrawable(getResources().getDrawable(
 
 R.drawable.right));
 
 }
 
 });
 
 mButton02.setOnClickListener(new Button.OnClickListener() {
 
 @Override
 
 public void onClick(View v) {
 
 mImageView01.setImageDrawable(getResources().getDrawable(
 
 R.drawable.left));
 
 }
 
 });
 
 }
 
 }
 复制代码
 
 结果:
 
  ImageButton 的堆叠应用
 
 
 ①新建项目
 ②准备三张 png 图片
 
  ③修改 main.xml 布局,添加 UI 元素
 <?xml version="1.0" encoding="utf-8"?>
 
 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
 android
  rientation="vertical" 
 android:layout_width="fill_parent"
 
 android:layout_height="fill_parent"
 
 >
 
 <ImageButton
 
 android:id="@+id/myImageButton_Back"
 
 android:state_focused="true"
 
 android:layout_width="fill_parent"
 
 android:layout_height="wrap_content"
 
 android:layout_x="0px"
 
 android:layout_y="36px"
 
 
 
 />
 
 <ImageButton
 
 android:id="@+id/myImageButton_Photo"
 
 android:state_focused="true"
 
 android:layout_width="fill_parent"
 
 android:layout_height="wrap_content"
 
 android:layout_x="0px"
 
 android:layout_y="36px"
 
 />
 
 </AbsoluteLayout>
 复制代码
 ④修改 mainActivity.java
 package zyf.Ex_Ctrl_7_B;
 
 import android.app.Activity;
 
 import android.os.Bundle;
 
 import android.view.View;
 
 import android.widget.ImageButton;
 
 
 
 public class Ex_Ctrl_7_B extends Activity {
 
 /** Called when the activity is first created. */
 
 /*声明 ImageButton*/
 
 private ImageButton back_Imagebutton,photo_Imagebutton;
 
 private boolean Tag=true;
 
 @Override
 
 public void onCreate(Bundle savedInstanceState) {
 
 super.onCreate(savedInstanceState);
 
 setContentView(R.layout.main);
 
 /*从XML中获取控件对象*/
 
 back_Imagebutton=(ImageButton)findViewById(R.id.myImageButton_Back);
 
 photo_Imagebutton=(ImageButton)findViewById(R.id.myImageButton_Photo);
 
 
 
 
 
 //设置默认的背景图片
 
 back_Imagebutton.setBackgroundResource(R.drawable.left);
 
 photo_Imagebutton.setBackgroundResource(R.drawable.photo);
 
 //给ImageButton设置事件监听器
 
 photo_Imagebutton.setOnClickListener(new ImageButton.OnClickListener(){
 
 @Override
 
 public void onClick(View v) {
 
 // TODO Auto-generated method stub
 
 Tag=!Tag;//更改背景图片
 
 if(Tag){
 
 back_Imagebutton.setBackgroundResource(R.drawable.right);
 
 }else{
 
 back_Imagebutton.setBackgroundResource(R.drawable.left);
 
 }
 
 }
 
 });
 
 }
 
 }
 复制代码
 
 结果:
 
  自定义下拉菜单
 Spinner 与setDropDownViewResource
 
 
 ①新建项目,在 res 目录下新建一个 anim 文件夹,存放动画效果用,在其中新建一个 my_anim.xml 文件
 <?xml version="1.0" encoding="utf-8"?>
 
 <set xmlns:android="http://schemas.android.com/apk/res/android">
 
 <translate
 
 android:fromXDelta="0"
 
 android:toXDelta="-100%p"
 
 android:duration="300">
 
 </translate><!--位置转换动画效果 -->
 
 <alpha
 
 android:fromAlpha="3.0"
 
 android:toAlpha="6.0"
 
 android:duration="300">
 
 </alpha><!--透明度转换动画效果 -->
 
 </set>
 复制代码
 ②在 res 目录下的 layout 文件夹中新建一个 myspinner_dropdown.xml 文件,用来存放下拉菜单弹出内容的布局
 
 
 
 <?xml version="1.0" encoding="utf-8"?>
 
 <TextView
 
 xmlns:android="http://schemas.android.com/apk/res/android"
 
 android:id="@+id/text1"
 
 android:layout_width="wrap_content"
 
 android:layout_height="24sp"
 
 android:singleLine="true"
 
 style="?android:attr/spinnerDropDownItemStyle"
 
 />
 复制代码
 ③修改 main.xml,添加 UI 元素
 <?xml version="1.0" encoding="utf-8"?>
 
 <LinearLayout
 
 android:id="@+id/widget28"
 
 android:layout_width="fill_parent"
 
 android:layout_height="fill_parent"
 
 android:orientation="vertical"
 
 xmlns:android="http://schemas.android.com/apk/res/android">
 
 <TextView
 
 android:id="@+id/TextView_Show"
 
 android:layout_width="fill_parent"
 
 
 
 android:layout_height="wrap_content"
 
 android:text="你选择的是"
 
 android:textSize="25sp">
 
 </TextView>
 
 <Spinner
 
 android:id="@+id/spinner_City"
 
 android:layout_width="fill_parent"
 
 android:layout_height="wrap_content">
 
 </Spinner><!-- 定义一个下拉菜单 -->
 
 </LinearLayout>
 复制代码
 ④定义一个下拉菜单
 private Spinner mySpinner;
 
 /* 以 findViewById() 取得对象 */
 
 mySpinner = (Spinner) findViewById(R.id.spinner_City);
 复制代码
 ⑤定义一个字符串数组和一个 ArrayAdepter
 private static final String[] countriesStr =
 
 { " 北京市 ", " 上海市 ", " 天津市 ", " 重庆市 " };
 
 private ArrayAdapter<String> adapter;
 
 
 
 
 
 adapter=new ArrayAdapter<String>(this,
 
 android.R.layout.simple_spinner_item, countriesStr);
 复制代码
 6、给下拉菜单内容设置样式
 /* myspinner_dropdown 为自定义下拉菜单样式定义在 res/layout 目录下 */
 
 adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
 复制代码
 给下拉菜单设置内容适配器
 /* 将 ArrayAdapter 添加 Spinner 对象中 */
 
 mySpinner.setAdapter(adapter);
 复制代码
 给下拉菜单添加动画
 /* 取得Animation 定义在res/anim 目录下*/
 
 myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);
 
 /* 将mySpinner 运行Animation */
 
 mySpinner.startAnimation(myAnimation);
 复制代码
 mainActivity.java 完整代码
 package zyf.Ex_Ctrl_8;
 
 import android.app.Activity;
 
 import android.os.Bundle;
 
 import
 
 import
 
 import
 
 import
 
 import
 
 import
 
 import
 
 import
 
 android.view.MotionEvent;
 
 android.view.View;
 
 android.view.animation.Animation;
 
 android.view.animation.AnimationUtils;
 
 android.widget.AdapterView;
 
 android.widget.ArrayAdapter;
 
 android.widget.Spinner;
 
 android.widget.TextView;
 
 public class Ex_Ctrl_8 extends Activity {
 
 /** Called when the activity is first created. */
 
 private static final String[] countriesStr =
 
 { " 北京市 ", " 上海市 ", " 天津市 ", " 重庆市 " };
 
 private TextView myTextView;
 
 private Spinner mySpinner;
 
 private ArrayAdapter<String> adapter;
 
 Animation myAnimation;
 
 @Override
 
 public void onCreate(Bundle savedInstanceState) {
 
 super.onCreate(savedInstanceState);
 
 /* 载入 main.xml Layout */
 
 setContentView(R.layout.main);
 
 /* 以 findViewById() 取得对象 */
 
 myTextView = (TextView) findViewById(R.id.TextView_Show);
 
 mySpinner = (Spinner) findViewById(R.id.spinner_City);
 
 /* 取得Animation 定义在res/anim 目录下*/
 
 myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);
 
 
 
 adapter=new ArrayAdapter<String>(this,
 
 android.R.layout.simple_spinner_item, countriesStr);
 
 /* myspinner_dropdown 为自定义下拉菜单样式定义在 res/layout 目录下 */
 
 adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
 
 /* 将 ArrayAdapter 添加 Spinner 对象中 */
 
 mySpinner.setAdapter(adapter);
 
 /*下拉菜单弹出的内容选项被选中事件处理*/
 
 mySpinner.setOnItemSelectedListener(new
 
 Spinner.OnItemSelectedListener(){
 
 public void onItemSelected(AdapterView<?> arg0, View arg1,
 
 int arg2, long arg3) {
 
 // TODO Auto-generated method stub
 
 /* 将所选mySpinner 的值带入myTextView 中*/
 
 myTextView.setText("您选择的是:"+ countriesStr[arg2]);
 
 /* 将mySpinner 显示*/
 
 
 
 arg0.setVisibility(View.VISIBLE);
 
 }
 
 public void onNothingSelected(AdapterView<?> arg0) {
 
 // TODO Auto-generated method stub
 
 myTextView.setText("NONE");
 
 }
 
 });
 
 /*下拉菜单弹出的内容选项 触屏事件处理*/
 
 mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
 
 public boolean onTouch(View v, MotionEvent event) {
 
 // TODO Auto-generated method stub
 
 /* 将mySpinner 运行Animation */
 
 v.startAnimation(myAnimation);
 
 /* 将mySpinner 隐藏*/
 
 v.setVisibility(View.INVISIBLE);
 
 return false;
 
 }
 
 });
 
 /*下拉菜单弹出的内容选项 焦点改变事件处理*/
 
 mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
 
 public void onFocusChange(View v, boolean hasFocus) {
 
 // TODO Auto-generated method stub
 
 }
 
 });
 
 }
 
 }
 复制代码
 
 结果:
 
 
  | 
 |