当前位置: 编程技术>移动开发
本页文章导读:
▪Handler与Message种,实现n秒后无操作自动消失功能 Handler与Message类,实现n秒后无操作自动消失功能
实现功能:某控件不操作10秒后,自动消失。如照相机变焦条出现后,无操作10秒自动隐藏。所用知识:handler message//定义变量private Effect.........
▪ 拖动一个控件在另一个控件(layout)下,并固定位置在几个位置显示 拖动一个控件在另一个控件(layout)上,并固定位置在几个位置显示
实现效果: 鼠标拖动btn SSS,SSS在水平的layout上移动。 当鼠标抬起 响应UP事件。SSS会自动移动到距离其最近的Btn上,与其重.........
▪ 关于内存储器管理,推荐一篇好文章 关于内存管理,推荐一篇好文章.
http://www.codeproject.com/KB/iPhone/avoidiphoneleaks.aspx?display=Mobile
......
[1]Handler与Message种,实现n秒后无操作自动消失功能
来源: 互联网 发布时间: 2014-02-18
Handler与Message类,实现n秒后无操作自动消失功能
实现功能:某控件不操作10秒后,自动消失。如照相机变焦条出现后,无操作10秒自动隐藏。
所用知识:handler message
//定义变量
private EffectInVisiableHandler mtimeHandler;
private final int MOBILE_QUERY = 1;
//程序启动时,初始化并发送消息
mtimeHandler = new EffectInVisiableHandler();
Message msg = mtimeHandler.obtainMessage(MOBILE_QUERY);
mtimeHandler.sendMessageDelayed(msg, 10000);
//在某控件的onclick或ontouch事件中,重置message,即从新计时开始。
Android 启动画面 SplashScreen
实现功能:某控件不操作10秒后,自动消失。如照相机变焦条出现后,无操作10秒自动隐藏。
所用知识:handler message
//定义变量
private EffectInVisiableHandler mtimeHandler;
private final int MOBILE_QUERY = 1;
//程序启动时,初始化并发送消息
mtimeHandler = new EffectInVisiableHandler();
Message msg = mtimeHandler.obtainMessage(MOBILE_QUERY);
mtimeHandler.sendMessageDelayed(msg, 10000);
//在某控件的onclick或ontouch事件中,重置message,即从新计时开始。
View.OnTouchListener touchCenterLayoutListener=new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
resetTime();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
};
private class EffectInVisiableHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case MOBILE_QUERY:
midLayoutInVisable(); //当10秒到达后,作相应的操作。
Log.i("","run**********************");
break;
}
}
}
public void resetTime() {
mtimeHandler.removeMessages(MOBILE_QUERY);
Message msg = mtimeHandler.obtainMessage(MOBILE_QUERY);
mtimeHandler.sendMessageDelayed(msg, 10000);
}Android 启动画面 SplashScreen
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000; //延迟五秒
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
// 为了减少代码使用匿名Handler创建一个延时的调用
public void run() {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
// 通过Intent打开最终真正的主界面Main这个Activity
SplashActivity.this.startActivity(i); // 启动Main界面
SplashActivity.this.finish(); // 关闭自己这个开场屏
}
}, SPLASH_DISPLAY_LENGHT);
}
}
[2] 拖动一个控件在另一个控件(layout)下,并固定位置在几个位置显示
来源: 互联网 发布时间: 2014-02-18
拖动一个控件在另一个控件(layout)上,并固定位置在几个位置显示
实现效果: 鼠标拖动btn SSS,SSS在水平的layout上移动。 当鼠标抬起 响应UP事件。SSS会自动移动到距离其最近的Btn上,与其重合。即SSS如图只存在五个固定的显示位置。
SSS响应setOnTouchListener事件。
在MotionEvent.ACTION_UP事件中,调用TranslateAnimation动画效果,将其从UP事件位置移动到最近的btn所在位置。
即在UP事件中,响应函数:
动画效果,将其移动到最近位置上
或者也可以这样计算:
全代码:
布局
但是这样有个问题:当点击EditText弹出输入法的时候,那个拖动条会回到初始的位置,这是何故?
实现效果: 鼠标拖动btn SSS,SSS在水平的layout上移动。 当鼠标抬起 响应UP事件。SSS会自动移动到距离其最近的Btn上,与其重合。即SSS如图只存在五个固定的显示位置。
SSS响应setOnTouchListener事件。
在MotionEvent.ACTION_UP事件中,调用TranslateAnimation动画效果,将其从UP事件位置移动到最近的btn所在位置。
即在UP事件中,响应函数:
private void setPosition() {
int positionPixel = (touchBtn.getLeft()+touchBtn.getRight())/2;
int positionIndex = (positionPixel)/btn[1].getWidth();
int toPosition = positionIndex*btn[1].getWidth()+touchBtn.getWidth()/2;
touchBtn.layout(positionIndex*btn[1].getWidth(), touchBtn.getTop(),positionIndex*btn[1].getWidth()+touchBtn.getWidth(),
touchBtn.getBottom());
MoveAction = new TranslateAnimation(positionPixel - toPosition,0,0,0);
MoveAction.setDuration(500);
touchBtn.startAnimation(MoveAction);
// touchBtn.invalidate();
}动画效果,将其移动到最近位置上
或者也可以这样计算:
/**
*获得最佳停留位置
*/
private void setBestPosition(View v) {
int width=v.getWidth();
int left = v.getLeft();
int selectedPosition = Math.round(1.0F*left/width);//四舍五入
int toPosition = selectedPosition*width;
v.layout(selectedPosition*width, v.getTop(),
selectedPosition*width+width, v.getBottom());
TranslateAnimation animation = new TranslateAnimation(left-toPosition,0,0,0);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(400);
animation.setFillAfter(true);
v.startAnimation(animation);
// v.invalidate();
}
全代码:
public class App extends Activity{
private static final String tag="App";
private Context context;
private FrameLayout container;
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=this;
container=(FrameLayout)findViewById(R.id.container);
btn=(Button)findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.tabswitcher_short);
btn.setOnTouchListener(touchLisener);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(tag,"btn clicked");
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
OnTouchListener touchLisener=new OnTouchListener() {
int lastX, lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
// int dy = (int) event.getRawY() - lastY;
int dy = 0;
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > container.getMeasuredWidth()) {
right = container.getMeasuredWidth();
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > container.getMeasuredHeight()) {
bottom = container.getMeasuredHeight();
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
setBestPosition(v);
break;
default:
break;
}
return false;
}
};
private void setBestPosition(View v) {
int width=v.getWidth();
int left = v.getLeft();
int selectedPosition = Math.round(1.0F*left/width);//四舍五入
int toPosition = selectedPosition*width;
v.layout(selectedPosition*width, v.getTop(),
selectedPosition*width+width, v.getBottom());
TranslateAnimation animation = new TranslateAnimation(left-toPosition,0,0,0);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(400);
animation.setFillAfter(true);
v.startAnimation(animation);
// v.invalidate();
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.ql.app"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TEST DRAG"
android:textSize="20sp"
/>
<FrameLayout android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<Button android:id="@+id/btn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="drag me!"
/>
</FrameLayout>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
但是这样有个问题:当点击EditText弹出输入法的时候,那个拖动条会回到初始的位置,这是何故?
[3] 关于内存储器管理,推荐一篇好文章
来源: 互联网 发布时间: 2014-02-18
关于内存管理,推荐一篇好文章.
http://www.codeproject.com/KB/iPhone/avoidiphoneleaks.aspx?display=Mobile
最新技术文章: