在项目设计和安卓程序开发过程中有时候我们需要试用编程动态生成界面,这样的好处是可以先读取屏幕大小然后动态生成满足屏幕大小的程序,本例的程序最终实现效果如下:例子为一个点菜初界面,左右滑动手指可以实现菜图片的动态切换,例子中使用了动画、手势操作和动态生成界面等核心技术。关键代码详解如下:
MainActivity.java中代码如下:
package com.study.androidfilp;
MainActivity.java中代码如下:
package com.study.androidfilp;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity implements OnGestureListener {
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
protected RelativeLayout rel_layout = null;
private GestureDetector detector = null;
private int screen_width = 0;
private int screen_hight = 0;
private int button_width_01 = 0;
private int button_hight_01 = 0;
private int screen_width = 0;
private int screen_hight = 0;
private int button_width_01 = 0;
private int button_hight_01 = 0;
private ViewFlipper fpr_filpper = null;
private Button btn_meat = null;
private Button btn_vegetable = null;
private Button btn_soup = null;
private Button btn_other = null;
private TextView txt_price = null;
private TextView txt_detil = null;
private Button btn_add = null;
private Button btn_order_detil = null;
private Button btn_meat = null;
private Button btn_vegetable = null;
private Button btn_soup = null;
private Button btn_other = null;
private TextView txt_price = null;
private TextView txt_detil = null;
private Button btn_add = null;
private Button btn_order_detil = null;
private final int btn_meat_id = 0x7f9001; //控件ID
private final int btn_vegetable_id = 0x7f9002;
private final int btn_soup_id = 0x7f9003;
private final int btn_other_id = 0x7f9004;
private final int btn_add_id = 0x7f9005;
private final int btn_order_detil_id = 0x7f9006;
private final int txt_price_id = 0x7f9007;
private final int txt_detil_id = 0x7f9008;
private final int fpr_filpper_id = 0x7f9009;
private final int btn_vegetable_id = 0x7f9002;
private final int btn_soup_id = 0x7f9003;
private final int btn_other_id = 0x7f9004;
private final int btn_add_id = 0x7f9005;
private final int btn_order_detil_id = 0x7f9006;
private final int txt_price_id = 0x7f9007;
private final int txt_detil_id = 0x7f9008;
private final int fpr_filpper_id = 0x7f9009;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题
this.initLayout();
setContentView(rel_layout);
this.getScreensDetil();
this.initComponents();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题
this.initLayout();
setContentView(rel_layout);
this.getScreensDetil();
this.initComponents();
}
private void getScreensDetil() { //读取屏幕大小
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
screen_width = metric.widthPixels;
screen_hight = metric.heightPixels;
this.button_hight_01 = screen_hight / 5;
this.button_width_01 = screen_width / 10;
System.out.println("Screen detil:" + screen_width + "*" + screen_hight);
}
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
screen_width = metric.widthPixels;
screen_hight = metric.heightPixels;
this.button_hight_01 = screen_hight / 5;
this.button_width_01 = screen_width / 10;
System.out.println("Screen detil:" + screen_width + "*" + screen_hight);
}
private void initLayout() {
this.rel_layout = new RelativeLayout(this);
this.rel_layout.setBackgroundColor(getResources().getColor(
android.R.color.black));
}
this.rel_layout = new RelativeLayout(this);
this.rel_layout.setBackgroundColor(getResources().getColor(
android.R.color.black));
}
private void initComponents() {
// detector
this.detector = new GestureDetector(this, this);
// detector
this.detector = new GestureDetector(this, this);
// btn_meat
this.btn_meat = new Button(this);
this.btn_meat.setId(this.btn_meat_id);
this.btn_meat.setText(R.string.btn_meat_mess);
RelativeLayout.LayoutParams lp_btn_meat = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_meat.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp_btn_meat.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.rel_layout.addView(btn_meat, lp_btn_meat);
this.btn_meat = new Button(this);
this.btn_meat.setId(this.btn_meat_id);
this.btn_meat.setText(R.string.btn_meat_mess);
RelativeLayout.LayoutParams lp_btn_meat = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_meat.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp_btn_meat.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.rel_layout.addView(btn_meat, lp_btn_meat);
// btn_vegetable
this.btn_vegetable = new Button(this);
this.btn_vegetable.setId(this.btn_vegetable_id);
this.btn_vegetable.setText(R.string.btn_vegetable_mess);
RelativeLayout.LayoutParams lp_btn_vegetable = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_vegetable.addRule(RelativeLayout.BELOW, this.btn_meat_id);
lp_btn_vegetable.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
this.rel_layout.addView(btn_vegetable, lp_btn_vegetable);
this.btn_vegetable = new Button(this);
this.btn_vegetable.setId(this.btn_vegetable_id);
this.btn_vegetable.setText(R.string.btn_vegetable_mess);
RelativeLayout.LayoutParams lp_btn_vegetable = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_vegetable.addRule(RelativeLayout.BELOW, this.btn_meat_id);
lp_btn_vegetable.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
this.rel_layout.addView(btn_vegetable, lp_btn_vegetable);
// btn_soup
this.btn_soup = new Button(this);
this.btn_soup.setId(this.btn_soup_id);
this.btn_soup.setText(R.string.btn_soup_mess);
RelativeLayout.LayoutParams lp_btn_soup = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_soup.addRule(RelativeLayout.BELOW, this.btn_vegetable_id);
lp_btn_soup.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
this.rel_layout.addView(btn_soup, lp_btn_soup);
this.btn_soup = new Button(this);
this.btn_soup.setId(this.btn_soup_id);
this.btn_soup.setText(R.string.btn_soup_mess);
RelativeLayout.LayoutParams lp_btn_soup = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_soup.addRule(RelativeLayout.BELOW, this.btn_vegetable_id);
lp_btn_soup.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
this.rel_layout.addView(btn_soup, lp_btn_soup);
// btn_other
this.btn_other = new Button(this);
this.btn_other.setId(this.btn_other_id);
this.btn_other.setText(R.string.btn_other_mess);
RelativeLayout.LayoutParams lp_btn_other = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_other.addRule(RelativeLayout.BELOW, this.btn_soup_id);
lp_btn_other.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
this.rel_layout.addView(btn_other, lp_btn_other);
this.btn_other = new Button(this);
this.btn_other.setId(this.btn_other_id);
this.btn_other.setText(R.string.btn_other_mess);
RelativeLayout.LayoutParams lp_btn_other = new RelativeLayout.LayoutParams(
this.button_width_01, this.button_hight_01);
lp_btn_other.addRule(RelativeLayout.BELOW, this.btn_soup_id);
lp_btn_other.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
this.rel_layout.addView(btn_other, lp_btn_other);
// fpr_filpper
this.fpr_filpper = new ViewFlipper(this);
this.fpr_filpper.setId(this.fpr_filpper_id);
this.fpr_filpper.setBackgroundColor(getResources().getColor(
android.R.color.black));
RelativeLayout.LayoutParams lp_fpr = new RelativeLayout.LayoutParams(
(screen_width / 2), screen_hight);
lp_fpr.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp_fpr.addRule(RelativeLayout.RIGHT_OF, this.btn_meat_id);
this.rel_layout.addView(this.fpr_filpper, lp_fpr);
this.fpr_filpper.addView(getImageView(R.drawable.scjc));
this.fpr_filpper.addView(getImageView(R.drawable.sckxc));
this.fpr_filpper.addView(getImageView(R.drawable.ttclj));
this.fpr_filpper.addView(getImageView(R.drawable.xcr));
this.fpr_filpper.addView(getImageView(R.drawable.yxqz));
this.fpr_filpper = new ViewFlipper(this);
this.fpr_filpper.setId(this.fpr_filpper_id);
this.fpr_filpper.setBackgroundColor(getResources().getColor(
android.R.color.black));
RelativeLayout.LayoutParams lp_fpr = new RelativeLayout.LayoutParams(
(screen_width / 2), screen_hight);
lp_fpr.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp_fpr.addRule(RelativeLayout.RIGHT_OF, this.btn_meat_id);
this.rel_layout.addView(this.fpr_filpper, lp_fpr);
this.fpr_filpper.addView(getImageView(R.drawable.scjc));
this.fpr_filpper.addView(getImageView(R.drawable.sckxc));
this.fpr_filpper.addView(getImageView(R.drawable.ttclj));
this.fpr_filpper.addView(getImageView(R.drawable.xcr));
this.fpr_filpper.addView(getImageView(R.drawable.yxqz));
// txt_price
this.txt_price = new TextView(this);
this.txt_price.setText("单价为:18.0元");
this.txt_price.setTextColor(getResources().getColor(
android.R.color.white));
this.txt_price.setId(this.txt_price_id);
RelativeLayout.LayoutParams lp_txt_price = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp_txt_price.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp_txt_price.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.txt_price, lp_txt_price);
this.txt_price = new TextView(this);
this.txt_price.setText("单价为:18.0元");
this.txt_price.setTextColor(getResources().getColor(
android.R.color.white));
this.txt_price.setId(this.txt_price_id);
RelativeLayout.LayoutParams lp_txt_price = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp_txt_price.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp_txt_price.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.txt_price, lp_txt_price);
// txt_detil
this.txt_detil = new TextView(this);
this.txt_detil.setText("This is a delicous meat...");
this.txt_detil.setTextColor(getResources().getColor(
android.R.color.white));
this.txt_detil.setId(this.txt_detil_id);
RelativeLayout.LayoutParams lp_txt_detil = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (screen_hight / 5 * 2));
lp_txt_detil.addRule(RelativeLayout.BELOW, this.txt_price_id);
lp_txt_detil.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.txt_detil, lp_txt_detil);
this.txt_detil = new TextView(this);
this.txt_detil.setText("This is a delicous meat...");
this.txt_detil.setTextColor(getResources().getColor(
android.R.color.white));
this.txt_detil.setId(this.txt_detil_id);
RelativeLayout.LayoutParams lp_txt_detil = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (screen_hight / 5 * 2));
lp_txt_detil.addRule(RelativeLayout.BELOW, this.txt_price_id);
lp_txt_detil.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.txt_detil, lp_txt_detil);
// btn_add
this.btn_add = new Button(this);
this.btn_add.setText(R.string.btn_add_mess);
this.btn_add.setTextColor(getResources()
.getColor(android.R.color.white));
this.btn_add.setId(this.btn_add_id);
RelativeLayout.LayoutParams lp_btn_add = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (screen_hight / 5));
lp_btn_add.addRule(RelativeLayout.BELOW, this.txt_detil_id);
lp_btn_add.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.btn_add, lp_btn_add);
this.btn_add = new Button(this);
this.btn_add.setText(R.string.btn_add_mess);
this.btn_add.setTextColor(getResources()
.getColor(android.R.color.white));
this.btn_add.setId(this.btn_add_id);
RelativeLayout.LayoutParams lp_btn_add = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (screen_hight / 5));
lp_btn_add.addRule(RelativeLayout.BELOW, this.txt_detil_id);
lp_btn_add.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.btn_add, lp_btn_add);
// btn_order_detil
this.btn_order_detil = new Button(this);
this.btn_order_detil.setText(R.string.btn_order_detil_mess);
this.btn_order_detil.setTextColor(getResources().getColor(
android.R.color.white));
this.btn_order_detil.setId(this.btn_order_detil_id);
RelativeLayout.LayoutParams lp_btn_order_detil = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (screen_hight / 5));
lp_btn_order_detil.addRule(RelativeLayout.BELOW, this.btn_add_id);
lp_btn_order_detil
.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.btn_order_detil, lp_btn_order_detil);
}
this.btn_order_detil = new Button(this);
this.btn_order_detil.setText(R.string.btn_order_detil_mess);
this.btn_order_detil.setTextColor(getResources().getColor(
android.R.color.white));
this.btn_order_detil.setId(this.btn_order_detil_id);
RelativeLayout.LayoutParams lp_btn_order_detil = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (screen_hight / 5));
lp_btn_order_detil.addRule(RelativeLayout.BELOW, this.btn_add_id);
lp_btn_order_detil
.addRule(RelativeLayout.RIGHT_OF, this.fpr_filpper_id);
this.rel_layout.addView(this.btn_order_detil, lp_btn_order_detil);
}
private View getImageView(int id) {
ImageView iv = new ImageView(this);
iv.setImageResource(id);
return iv;
}
ImageView iv = new ImageView(this);
iv.setImageResource(id);
return iv;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if ((e1.getX() - e2.getX()) < -120.0) { // 向右滑动
this.fpr_filpper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.fpr_filpper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.fpr_filpper.showNext();
return true;
} else if ((e1.getX() - e2.getX()) > 120) { // 向左滑动
this.fpr_filpper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.fpr_filpper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.fpr_filpper.showPrevious();
return true;
}
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if ((e1.getX() - e2.getX()) < -120.0) { // 向右滑动
this.fpr_filpper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.fpr_filpper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.fpr_filpper.showNext();
return true;
} else if ((e1.getX() - e2.getX()) > 120) { // 向左滑动
this.fpr_filpper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.fpr_filpper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.fpr_filpper.showPrevious();
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
push_left_in.xml 从左边进入的动画效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="1000" />
<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
android:duration="1000" />
</set>
puh_left_out.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="1000" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
android:duration="1000" />
</set>
push_right_in.xml从右边切入的动画效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="1000" />
<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
android:duration="1000" />
</set>
push_right_out.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="1000" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
android:duration="1000" />
</set>
在程序res中anim文件夹内引入了5张图片用于演示切换效果。