标签:
1 关于常见的对话框,主要有:
常见的对话框,单选对话框,多选对话框,进度条对话框(转圈类型的),带进度条的对话框。
案例结构:
完成如下结构的案例,将所有的案例都测试一下:
2 编写MainActivity,代码如下:
package com.itheima.dialog;
import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.View; import android.widget.Toast;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void click1(View view) { // 对话框的创建器 AlertDialog.Builder builder = new Builder(this); builder.setTitle("我是对话框"); builder.setMessage("对话框显示的内容"); // 设置点击确定按钮后制定的动作 builder.setPositiveButton("确定", new OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "确定被点击了", 0).show(); } }); builder.setNegativeButton("取消", new OnClickListener() {// 设置取消按钮
@Override public void onClick(DialogInterface dialog, int which) { // 什么都不写默认实现的就是关闭掉对话框 Toast.makeText(getApplicationContext(), "点击了取消按钮", Toast.LENGTH_LONG).show(); } }); builder.setCancelable(false); builder.create().show(); }
/** * 单选对话框 * * @param view */ public void click2(View view) { // 对话框的创建器 AlertDialog.Builder builder = new Builder(this); builder.setTitle("请选择您的性别"); final String[] items = { "男", "女", "未知" }; //这里的1表示默认选中的是哪个,0:表示选中的是第一个 builder.setSingleChoiceItems(items, 1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "您的性别:" + items[which], 0).show(); dialog.dismiss(); } }); builder.create().show(); }
/** * 多选对话框 * @param view */ public void click3(View view) { // 对话框的创建器 AlertDialog.Builder builder = new Builder(this); builder.setTitle("请选择你最爱吃的水果"); final String[] items = { "苹果", "梨", "菠萝", "香蕉", "黄瓜" }; final boolean[] result = new boolean[] { true, false, true, false,false}; builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getApplicationContext(), items[which] + isChecked, 0).show(); result[which] = isChecked; } }); builder.setPositiveButton("提交", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < result.length; i++) { if (result[i]) { sb.append(items[i] + ","); } } Toast.makeText(getApplicationContext(), "您选中了," + sb.toString(), 0).show(); } }); // builder.create().show();
builder.show(); }
// 进度条对话框 public void click4(View view) { ProgressDialog pd = new ProgressDialog(this); pd.setTitle("提醒"); pd.setMessage("正在加载数据...请稍等。"); pd.show(); }
// 带进度的进度条对话框 public void click5(View view) { final ProgressDialog pd = new ProgressDialog(this); pd.setTitle("提醒"); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMax(100); pd.setMessage("正在加载数据...请稍等。"); pd.show(); new Thread() { public void run() { for (int i = 0; i < 100; i++) { try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } pd.setProgress(i); } pd.dismiss(); }; }.start(); } }
|
==============================================================================
1 光传感器
编写布局文件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" tools:context=".MainActivity" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" />
</RelativeLayout> |
2 编写MainActivity,代码如下:
package com.itheima.sensor;
import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle;
public class MainActivity extends Activity { private SensorManager sm; private MyListener listener;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sm = (SensorManager) getSystemService(SENSOR_SERVICE); //光线传感器 Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT); listener = new MyListener(); sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI); }
private class MyListener implements SensorEventListener { public void onSensorChanged(SensorEvent event) { float light = event.values[0]; System.out.println("light:" + light); }
public void onAccuracyChanged(Sensor sensor, int accuracy) {
} }
@Override protected void onDestroy() { sm.unregisterListener(listener); listener = null; super.onDestroy(); } } |
==============================================================================
1 Android指南针,案例效果:
2 编写布局文件,代码如下(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:background="#000000" tools:context=".MainActivity" >
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/zn" />
</RelativeLayout> |
3 编写MainActivity,代码如下:
package com.itheima.sensor;
import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.ImageView;
public class MainActivity extends Activity { private SensorManager sm; private MyListener listener; private ImageView iv;
@SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sm = (SensorManager) getSystemService(SENSOR_SERVICE); iv = (ImageView) findViewById(R.id.iv); //方向传感器 Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION); listener = new MyListener(); sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME); }
private class MyListener implements SensorEventListener { float lastangle = 0; @Override public void onSensorChanged(SensorEvent event) { // 0=North, 90=East, 180=South, 270=West float angle = event.values[0];//手机与正北方向的夹角 System.out.println("angle:"+angle); RotateAnimation ra = new RotateAnimation(-lastangle, angle, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); iv.startAnimation(ra); lastangle = angle; }
@Override public void onAccuracyChanged(Sensor sensor, int accuracy) {
} }
@Override protected void onDestroy() { sm.unregisterListener(listener); listener = null; super.onDestroy(); } } |
补间动画主要包括以下几种:
A (旋转) B(透明度) C(位移) D(缩放)
编写一下案例:
1 Android布局文件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" tools:context=".MainActivity" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<Button android:onClick="rotate" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋转" />
<Button android:onClick="scale" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="缩放" />
<Button android:onClick="trans" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="位移" />
<Button android:onClick="alpha" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="透明度" />
<Button android:onClick="set" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="组合动画" /> </LinearLayout>
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" />
</RelativeLayout> |
2 MainActivity代码如下:
package com.itheima.tween;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); }
// 透明度动画 public void alpha(View view) { // 最开始的透明度到最后的透明度,从0.0f到1.0f,从透明到不透明 Animation aa = new AlphaAnimation(0.0f, 1.0f); // 设置动画播放的时间 aa.setDuration(2000); // 设置动画重复播放的次数,下面表示重复播放3次,表示重复播放2,如果是 // -1(Animation.INFINITE)表示一直重复播放 aa.setRepeatCount(3); // aa.setRepeatCount(Animation.INFINITE); // 如果不指定这个值,默认是重复播放的。下面表示:透明-->不透明-->透明 aa.setRepeatMode(Animation.REVERSE); // true:界面为动画完成之后的效果 aa.setFillAfter(true); // 开始播放 iv.startAnimation(aa); }
/** * 位移动画 * * @param view */ public void trans(View view) { // 下面表示x轴从0.0f-->1.0f;0.0f-->1.0f // android.view.animation.TranslateAnimation.TranslateAnimation(int // fromXType, float fromXValue, int toXType, float toXValue, int // fromYType, float fromYValue, int toYType, float toYValue) TranslateAnimation ta = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, // 相对于父窗体 0.0f, // 如果是320宽度的模拟器。这里0.0f表示是是父窗体的0% Animation.RELATIVE_TO_PARENT, // 还是相对于父窗体 1.0f, // 表示父亲的100% Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); ta.setDuration(2000); // 设置时间间隔 ta.setRepeatCount(-1); // -1表示重复的操作 // 倒叙播放 ta.setRepeatMode(Animation.REVERSE); iv.startAnimation(ta); }
// 缩放动画 public void scale(View view) { ScaleAnimation sa = new ScaleAnimation(0.1f, // 缩放的时候最开始的比例 2.0f, // 上面这两个参数x周表示从0.1倍到2倍 0.1f, 2.0f, // y轴从0.1-->2.0倍 Animation.RELATIVE_TO_SELF, // 后面4个参数的组合表示从自己中心点开始缩小放大 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(2000); // 设置时间间隔 sa.setRepeatCount(1); // -1表示重复的操作 // 倒叙播放 sa.setRepeatMode(Animation.REVERSE); iv.startAnimation(sa); }
// 旋转动画 public void rotate(View view) { RotateAnimation ra = new RotateAnimation( 0, // 开始的角度 360, // 旋转的解读 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); iv.startAnimation(ra); }
//动画组合(包含多种动画) public void set(View view) { AnimationSet set = new AnimationSet(false); TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); ta.setDuration(2000); ta.setRepeatCount(1); ta.setRepeatMode(Animation.REVERSE); ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); set.addAnimation(ra); //set.addAnimation(ta); set.addAnimation(sa); iv.startAnimation(set); } } |
=============================================================================
1 除了通过代码的方式制作补间动画之外,还可以通过xml的方式制作补间动画。
案例:
2 下面通过如下结构的代码编写出上面的案例:
3 编写的布局文件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" tools:context=".MainActivity" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<Button android:onClick="rotate" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋转" />
<Button android:onClick="scale" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="缩放" />
<Button android:onClick="trans" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="位移" />
<Button android:onClick="alpha" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="透明度" />
<Button android:onClick="set" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="组合动画" /> </LinearLayout>
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" />
</RelativeLayout> |
4 编写透明度的xml文件alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- android:fromAlpha="0.0" 开始的透明度 android:toAlpha="1.0" 结束的透明度 android:duration="2000" 动画播放的时间 android:repeatCount="1" 动画重复的次数 android:repeatMode="reverse" 重复的模式 android:fillAfter="true" --> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" android:repeatCount="2" android:repeatMode="reverse" android:fillAfter="true">
</alpha> |
5 编写旋转的xml文件rotate.xml
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" >
</rotate> |
6 编写放大缩小的xml文件scale.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- android:fromXScale="0.1" android:toXScale="2.0" android:fromYScale="0.1" android:toYScale="2.0" android:duration="2000" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" --> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.1" android:toXScale="2.0" android:fromYScale="0.1" android:toYScale="2.0" android:duration="2000" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse">
</scale> |
7 编写位移的xml文件trans.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- android:fromXDelta="-50%p" 左侧 android:toXDelta="50%p" 右侧 android:fromYDelta="0" 表示y轴方向上不变化 android:toYDelta="0" android:duration="2000" 播放2秒 android:repeatCount="1" 重复1次 android:repeatMode="reverse" --> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="-50%p" android:toXDelta="50%p" android:fromYDelta="0" android:toYDelta="0" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse">
</translate> |
8 编写组合动画set.xml
<?xml version="1.0" encoding="utf-8"?> <set> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromAlpha="0.0" android:repeatCount="1" android:repeatMode="reverse" android:toAlpha="1.0" > </alpha>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toDegrees="360" > </rotate>
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0.1" android:fromYScale="0.1" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toXScale="2.0" android:toYScale="2.0" > </scale>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="-50%p" android:fromYDelta="0" android:repeatCount="1" android:repeatMode="reverse" android:toXDelta="50%p" android:toYDelta="0" > </translate>
</set> |
9 编写MainActivity,代码如下:
package com.itheima.tween;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); }
// 透明度动画 public void alpha(View view) { Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha); iv.startAnimation(aa); }
/** * 位移动画 * * @param view */ public void trans(View view) { Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans); iv.startAnimation(ta); }
// 缩放动画 public void scale(View view) { Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale); iv.startAnimation(sa); }
// 旋转动画 public void rotate(View view) { Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate); iv.startAnimation(ra); }
//动画组合(包含多种动画) public void set(View view) { Animation set = AnimationUtils.loadAnimation(this, R.anim.set); iv.startAnimation(set); } } |
清单文件略
版权声明:本文为博主原创文章,未经博主允许不得转载。
21_Android中常见对话框,光传感器,通过重力感应器编写出指南针应用,通过Jav代码的方式编写补间动画,通过XML的方式编写补间动画
标签:
原文地址:http://blog.csdn.net/tototuzuoquan/article/details/47013313