Android - 小案例1(升级版) (边学边共享)

延续上次发的小案例1,在小案例1是简单实现了单击View切换对应的背景颜色.

这次是可以通过滑动连续改变.比如手指在触摸屏幕时移动,经过我们指定的View

就会切换对应的背景颜色.没看过小案例1的新手可以前往一下链接...不想看的也

可以直接忽略。

http://www.cnblogs.com/cscs/archive/2010/06/02/1749906.html

好了..开始,先贴layout部分的代码

1 <?xml version="1.0" encoding="UTF-8"?>
2  <RelativeLayout android:paddingLeft="75dip" android:longClickable="true" android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
3  <View android:longClickable="true" android:layout_below="@+id/Button01" android:layout_height="40dip" android:background="#C0C0C0" android:id="@+id/View01" android:layout_width="40dip"></View>
4  <View android:longClickable="true" android:layout_toRightOf="@+id/View01" android:layout_height="40dip" android:background="#FFFF00" android:id="@+id/View02" android:layout_width="40dip"></View>
5 <View android:longClickable="true" android:layout_toRightOf="@+id/View02" android:layout_height="40dip" android:background="#B0E0E6" android:id="@+id/View03" android:layout_width="40dip"></View>
6 <View android:longClickable="true" android:layout_toRightOf="@+id/View03" android:layout_height="40dip" android:background="#6A5ACD" android:id="@+id/View04" android:layout_width="40dip"></View>
7 </RelativeLayout>
Layout的代码跟上次的有一点不同,就是在每个View上都加了个 android:longClickable="true"

有了这个就可以监听到MotionEvent.ACTION_MOVE事件, 就是按下屏幕并且移动..

下面贴后台代码部分。

package cn.Charles;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class TextColor extends Activity implements OnTouchListener {
RelativeLayout rtl;
View v1;
View v2;
View v3;
View v4;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Initialize();
}

protected void Initialize()
{
setContentView(R.layout.textcolorchange);
rtl
= (RelativeLayout)findViewById(R.id.RelativeLayout01);
rtl.setOnTouchListener(
this);
v1
= (View)findViewById(R.id.View01);
v1.setOnTouchListener(
this);
v2
= (View)findViewById(R.id.View02);
v2.setOnTouchListener(
this);
v3
= (View)findViewById(R.id.View03);
v3.setOnTouchListener(
this);
v4
= (View)findViewById(R.id.View04);
v4.setOnTouchListener(
this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
float Y = event.getRawY();
float X = event.getRawX();

if(Y > 50 && Y < 90 && X > 75 && X < 115)
{
rtl.setBackgroundDrawable(v1.getBackground());

}

if(Y > 50 && Y < 90 && X > 115 && X < 155)
{
rtl.setBackgroundDrawable(v2.getBackground());

}

if(Y > 50 && Y < 90 && X > 155 && X < 195)
{
rtl.setBackgroundDrawable(v3.getBackground());

}

if(Y > 50 && Y < 90 && X > 195 && X < 235)
{
rtl.setBackgroundDrawable(v4.getBackground());

}
return false;
}
}
实现的原理请看下图.

以上的做法只能我自己想出来的,因为不知道如何实现该效果,也找了Android好像没类似onmouseover的事件.

因为本人也刚学不久,如果知道的同学请告诉一下!

好了,就讲到这里 上班了!


 

posted @ 2010-06-05 09:21  cxc414  阅读(641)  评论(2编辑  收藏  举报