当前位置: 编程技术>移动开发
本页文章导读:
▪用户登陆时暗藏密码 用户登陆时隐藏密码
在做登陆时用到的有关输入的密码显示与隐藏的代码,虽然很简单,但有用的东西,直接当笔记写下来,吼吼,先看下效果:[img][/img][img][/img][img][/img]下面来看下这代.........
▪ 歌词悬浮在全部页面之上 歌词悬浮在所有页面之上
无意间在群里看到有朋友问过如何实现歌词悬浮等问题,再加小马本身也好奇这个是怎么实现的,所以专门找了下这方面的文章,找到一篇我本人觉得不错的文章.........
▪ 完整的抽屉式工具兑现 完整的抽屉式工具实现
先上图,哈哈,俺和俺老婆、哈哈运行时效果图:[img][/img]点击右边小按钮时效果图:[img][/img]工程结构图:[img][/img]SlidingDrawerDemoActivityActivity:package com.amaker.sliding;
im.........
[1]用户登陆时暗藏密码
来源: 互联网 发布时间: 2014-02-18
用户登陆时隐藏密码
在做登陆时用到的有关输入的密码显示与隐藏的代码,虽然很简单,但有用的东西,直接当笔记写下来,吼吼,先看下效果:
[img]
[/img]
[img]
[/img]
[img]
[/img]
下面来看下这代码有多简单,有用,小x就记下,不管多简单:
在做登陆时用到的有关输入的密码显示与隐藏的代码,虽然很简单,但有用的东西,直接当笔记写下来,吼吼,先看下效果:
[img]
[/img]
[img]
[/img]
[img]
[/img]
下面来看下这代码有多简单,有用,小x就记下,不管多简单:
package com.xiaoma.www;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
/**
* @Title: PasswordIsShowTestActivity.java
* @Package com.xiaoma.www
* @Description: 用户登陆时密码操作
* @author MZH
* @version V2.2
*/
public class PasswordIsShowTestActivity extends Activity {
private EditText username;
private EditText password;
private CheckBox cbBox ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
cbBox = (CheckBox)findViewById(R.id.checkBox1);
cbBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cbBox.isChecked()){
//HideReturnsTransformationMethod这个类朋友可以Ctrl跟踪下哦,吼吼
password.setTransformationMethod(
HideReturnsTransformationMethod.getInstance());
}else{
//下面这个类朋友们也可以自己跟下,很简单的
password.setTransformationMethod(
PasswordTransformationMethod.getInstance());
}
}
});
}
}
[2] 歌词悬浮在全部页面之上
来源: 互联网 发布时间: 2014-02-18
歌词悬浮在所有页面之上
无意间在群里看到有朋友问过如何实现歌词悬浮等问题,再加小马本身也好奇这个是怎么实现的,所以专门找了下这方面的文章,找到一篇我本人觉得不错的文章,所以转来与大家分享咯,呵,开始咱们的歌词悬浮实现:
小述:
这个歌词是在所有界面之上的
下面我们将这个效果解剖一下, 我认为主要有三个难点:
1. 歌词悬浮在所有页面之上
2. 歌词可以拖动位置
3 . 歌词的播放效果 (颜色覆盖)
对于第一点,首先想到的就是 WindowManager , 这个类可能不少人都用过, 一般用于获取屏幕宽度、高度,那么这次就要利用这个类来让我们的歌词永远置顶。通过查看API,我们看到,在WindowManager.LayoutParams类中,有好几个属性可以设置View置顶,此处,小马说一句,就是别忘了看到不懂的单词查下工具或API:
TYPE_SYSTEM_OVERLAY
Window type: system overlay windows, which need to be displayed on top of everything else.
TYPE_SYSTEM_ALERT
Window type: system window, such as low power alert.
TYPE_PHONE
These windows are normally placed above all applications, but behind the status bar.
下面我们来测试一下, 通过下面几句代码,就可以让一个View凌驾在所有View之上啦,吼吼:
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
TextView tv = new TextView(this);
wm.addView(tv, params);
这边需注意的是, WindowManager也是通过 getSystemService 来获取,但必须先 getApplicationContext, 否则就无效了。 直接WindowManager wm=(WindowManager)getSystemService(WINDOW_SERVICE); 这样是无效的 !! 还有一点就是,别忘了在Manifest.xml中添加权限:
view plaincopy to clipboardprint?
01.<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
现在我们这样做,我们已经可以让歌词永远置顶了。 但是不要得意,现在这样,结果是我们TextView在最顶层了, 然后你就会发现,页面上什么操作都不能做了, 在TextView下面的任何东西,你都点不了。 为了解决这个,我们必须加上flags参数,让当前的View失去焦点,从而让后面的页面获得焦点。代码如下:
view plaincopy to clipboardprint?
01.params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
加上这一句就可以了。 好了,下面要处理的,就是让歌词可以移动。应该如何做呢? 我们知道,想要让一个View对象在页面上可以移动,只要实现其onTouchEvent事件即可。
下面开始实现第二步: 歌词移动! 首先我们自定义一个TextView类:MyTextView, 该类继承自TextView, 并实现其中的onTouchEvent方法,来看一下代码:
@Override
public boolean onTouchEvent(MotionEvent event) {
//触摸点相对于屏幕左上角坐标
x = event.getRawX();
y = event.getRawY() - TOOL_BAR_HIGH;
Log.d(TAG, "------X: "+ x +"------Y:" + y);
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updatePosition();
break;
case MotionEvent.ACTION_UP:
updatePosition();
startX = startY = 0;
break;
}
return true;
}
//更新浮动窗口位置参数
private void updatePosition(){
// View的当前位置
params.x = (int)( x - startX);
params.y = (int) (y - startY);
wm.updateViewLayout(this, params);
}
其中getRawX、getRawY用于获取触摸点离屏幕左上角的距离。 而getX、getY用于获取触摸点离textView左上角的距离.两者相减,就是View左上角的坐标了。另外需要注意的是,在显示View这个View的时候,需要正确指定View的x,y坐标,否则拖动时会错位。
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = MyTextView.params;
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.FILL_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.alpha = 80;
params.gravity=Gravity.LEFT|Gravity.TOP;
//以屏幕左上角为原点,设置x、y初始值
params.x = 0;
params.y = 0;
tv = new MyTextView(TopFrame.this);
wm.addView(tv, params);
其中下面三句是关键:
params.gravity=Gravity.LEFT|Gravity.TOP;
//以屏幕左上角为原点,设置x、y初始值
.params.x = 0;
params.y = 0;
现在这样的话,就可以实现View的移动了。下面实现第三步: 歌词的播放效果。那么本例仅仅做一个循环, 实际音乐播放器要复杂些,需要根据歌剧的长度及时间间隔,来计算歌词的覆盖速度, 再根据这个速度来覆盖歌词,呈现给用户。要实现歌词播放的效果,需要用到画笔Paint, 还要用到Shader, 还有一个就是UI刷新的问题。一起来看下代码:
@Override
.protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
float1 += 0.001f;
float2 += 0.001f;
if(float2 > 1.0){
float1 = 0.0f;
float2 = 0.01f;
}
this.setText("");
float len = this.getTextSize() * text.length();
Shader shader = new LinearGradient(0, 0, len, 0,
new int[] { Color.YELLOW, Color.RED }, new float[]{float1, float2},
TileMode.CLAMP);
Paint p = new Paint();
p.setShader(shader);
// 下面这句才控制歌词大小
p.setTextSize(20f);
p.setTypeface(Typeface.DEFAULT_BOLD);
//此处x,y坐标也要注意,尤其是y坐标,要与字体大小协调
canvas.drawText(text, 0, 20, p);
}
最后,看下布局文件及全局文件的配置吧,吼吼:
main.xml
TopFrame.java
无意间在群里看到有朋友问过如何实现歌词悬浮等问题,再加小马本身也好奇这个是怎么实现的,所以专门找了下这方面的文章,找到一篇我本人觉得不错的文章,所以转来与大家分享咯,呵,开始咱们的歌词悬浮实现:
小述:
这个歌词是在所有界面之上的
下面我们将这个效果解剖一下, 我认为主要有三个难点:
1. 歌词悬浮在所有页面之上
2. 歌词可以拖动位置
3 . 歌词的播放效果 (颜色覆盖)
对于第一点,首先想到的就是 WindowManager , 这个类可能不少人都用过, 一般用于获取屏幕宽度、高度,那么这次就要利用这个类来让我们的歌词永远置顶。通过查看API,我们看到,在WindowManager.LayoutParams类中,有好几个属性可以设置View置顶,此处,小马说一句,就是别忘了看到不懂的单词查下工具或API:
TYPE_SYSTEM_OVERLAY
Window type: system overlay windows, which need to be displayed on top of everything else.
TYPE_SYSTEM_ALERT
Window type: system window, such as low power alert.
TYPE_PHONE
These windows are normally placed above all applications, but behind the status bar.
下面我们来测试一下, 通过下面几句代码,就可以让一个View凌驾在所有View之上啦,吼吼:
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
TextView tv = new TextView(this);
wm.addView(tv, params);
这边需注意的是, WindowManager也是通过 getSystemService 来获取,但必须先 getApplicationContext, 否则就无效了。 直接WindowManager wm=(WindowManager)getSystemService(WINDOW_SERVICE); 这样是无效的 !! 还有一点就是,别忘了在Manifest.xml中添加权限:
view plaincopy to clipboardprint?
01.<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
现在我们这样做,我们已经可以让歌词永远置顶了。 但是不要得意,现在这样,结果是我们TextView在最顶层了, 然后你就会发现,页面上什么操作都不能做了, 在TextView下面的任何东西,你都点不了。 为了解决这个,我们必须加上flags参数,让当前的View失去焦点,从而让后面的页面获得焦点。代码如下:
view plaincopy to clipboardprint?
01.params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
加上这一句就可以了。 好了,下面要处理的,就是让歌词可以移动。应该如何做呢? 我们知道,想要让一个View对象在页面上可以移动,只要实现其onTouchEvent事件即可。
下面开始实现第二步: 歌词移动! 首先我们自定义一个TextView类:MyTextView, 该类继承自TextView, 并实现其中的onTouchEvent方法,来看一下代码:
@Override
public boolean onTouchEvent(MotionEvent event) {
//触摸点相对于屏幕左上角坐标
x = event.getRawX();
y = event.getRawY() - TOOL_BAR_HIGH;
Log.d(TAG, "------X: "+ x +"------Y:" + y);
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updatePosition();
break;
case MotionEvent.ACTION_UP:
updatePosition();
startX = startY = 0;
break;
}
return true;
}
//更新浮动窗口位置参数
private void updatePosition(){
// View的当前位置
params.x = (int)( x - startX);
params.y = (int) (y - startY);
wm.updateViewLayout(this, params);
}
其中getRawX、getRawY用于获取触摸点离屏幕左上角的距离。 而getX、getY用于获取触摸点离textView左上角的距离.两者相减,就是View左上角的坐标了。另外需要注意的是,在显示View这个View的时候,需要正确指定View的x,y坐标,否则拖动时会错位。
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = MyTextView.params;
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.FILL_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.alpha = 80;
params.gravity=Gravity.LEFT|Gravity.TOP;
//以屏幕左上角为原点,设置x、y初始值
params.x = 0;
params.y = 0;
tv = new MyTextView(TopFrame.this);
wm.addView(tv, params);
其中下面三句是关键:
params.gravity=Gravity.LEFT|Gravity.TOP;
//以屏幕左上角为原点,设置x、y初始值
.params.x = 0;
params.y = 0;
现在这样的话,就可以实现View的移动了。下面实现第三步: 歌词的播放效果。那么本例仅仅做一个循环, 实际音乐播放器要复杂些,需要根据歌剧的长度及时间间隔,来计算歌词的覆盖速度, 再根据这个速度来覆盖歌词,呈现给用户。要实现歌词播放的效果,需要用到画笔Paint, 还要用到Shader, 还有一个就是UI刷新的问题。一起来看下代码:
@Override
.protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
float1 += 0.001f;
float2 += 0.001f;
if(float2 > 1.0){
float1 = 0.0f;
float2 = 0.01f;
}
this.setText("");
float len = this.getTextSize() * text.length();
Shader shader = new LinearGradient(0, 0, len, 0,
new int[] { Color.YELLOW, Color.RED }, new float[]{float1, float2},
TileMode.CLAMP);
Paint p = new Paint();
p.setShader(shader);
// 下面这句才控制歌词大小
p.setTextSize(20f);
p.setTypeface(Typeface.DEFAULT_BOLD);
//此处x,y坐标也要注意,尤其是y坐标,要与字体大小协调
canvas.drawText(text, 0, 20, p);
}
最后,看下布局文件及全局文件的配置吧,吼吼:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yfz"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TopFrame"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-sdk android:minSdkVersion="8"></uses-sdk>
</manifest> main.xml
main.xml
view plaincopy to clipboardprint?
.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/bt"
android:text=" 点我试试"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout> TopFrame.java
view plaincopy to clipboardprint?
package com.yfz;
import com.yfz.view.MyTextView;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
public class TopFrame extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.bt);
button.setOnClickListener(onclick);
}
private MyTextView tv = null;
OnClickListener onclick = new OnClickListener() {
@Override
public void onClick(View v) {
if(tv != null && tv.isShown()){
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(TopFrame.this.WINDOW_SERVICE);
wm.removeView(tv);
}
show();
}
};
private void show(){
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
MyTextView.TOOL_BAR_HIGH = frame.top;
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = MyTextView.params;
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.FILL_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.alpha = 80;
params.gravity=Gravity.LEFT|Gravity.TOP;
//以屏幕左上角为原点,设置x、y初始值
params.x = 0;
params.y = 0;
tv = new MyTextView(TopFrame.this);
wm.addView(tv, params);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
} MyTextView.java
view plaincopy to clipboardprint?
package com.yfz.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.Shader.TileMode;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
.import android.widget.TextView;
import android.widget.Toast;
public class MyTextView extends TextView {
private final String TAG = MyTextView.class.getSimpleName();
public static int TOOL_BAR_HIGH = 0;
public static WindowManager.LayoutParams params = new WindowManager.LayoutParams();
private float startX;
private float startY;
private float x;
private float y;
private String text;
WindowManager wm = (WindowManager)getContext().getApplicationContext().getSystemService(getContext().WINDOW_SERVICE);
public MyTextView(Context context) {
super(context);
text = "世上只有妈妈好,有妈的孩子像块宝";
. this.setBackgroundColor(Color.argb(90, 150, 150, 150));
// 下面这句话在此并不是控制歌词大小,仅仅是为了控制背景大小,如果不设置的话,Paint字体大时会被遮挡
this.setTextSize(20f);
handler = new Handler();
handler.post(update);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//触摸点相对于屏幕左上角坐标 x = event.getRawX();
y = event.getRawY() - TOOL_BAR_HIGH;
Log.d(TAG, "------X: "+ x +"------Y:" + y);
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updatePosition();
break;
case MotionEvent.ACTION_UP:
updatePosition();
startX = startY = 0;
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
float1 += 0.001f;
float2 += 0.001f;
if(float2 > 1.0){
float1 = 0.0f;
float2 = 0.01f;
}
this.setText("");
float len = this.getTextSize() * text.length();
Shader shader = new LinearGradient(0, 0, len, 0,
new int[] { Color.YELLOW, Color.RED }, new float[]{float1, float2},
TileMode.CLAMP);
Paint p = new Paint();
p.setShader(shader);
// 下面这句才控制歌词大小
p.setTextSize(20f);
p.setTypeface(Typeface.DEFAULT_BOLD);
//此处x,y坐标也要注意,尤其是y坐标,要与字体大小协调
canvas.drawText(text, 0, 20, p);
}
private Runnable update = new Runnable() {
public void run() {
MyTextView.this.update();
handler.postDelayed(update, 3);
}
};
private void update(){
postInvalidate();
}
private float float1 = 0.0f;
private float float2 = 0.01f;
private Handler handler;
//更新浮动窗口位置参数
private void updatePosition(){
// View的当前位置
params.x = (int)( x - startX);
params.y = (int) (y - startY);
wm.updateViewLayout(this, params);
}
}
[3] 完整的抽屉式工具兑现
来源: 互联网 发布时间: 2014-02-18
完整的抽屉式工具实现
先上图,哈哈,俺和俺老婆、哈哈
运行时效果图:
[img]
[/img]
点击右边小按钮时效果图:
[img]
[/img]
工程结构图:
[img]
[/img]
SlidingDrawerDemoActivityActivity:
GridViewAdapter
grid.xml
main.xml
color.xml
配置文件:
先上图,哈哈,俺和俺老婆、哈哈
运行时效果图:
[img]
[/img]
点击右边小按钮时效果图:
[img]
[/img]
工程结构图:
[img]
[/img]
SlidingDrawerDemoActivityActivity:
package com.amaker.sliding;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.Toast;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
/**
* @Title: SlidingDrawerDemoActivity.java
* @Package com.xiaoma.www
* @Description: 抽屉式控件SlidingDrawer的显示与隐藏
* @author zzl
*/
public class SlidingDrawerDemoActivityActivity extends Activity {
//声明
private SlidingDrawer sDrawer ;
private GridView gvGridView ;
//点击小抽屉的小标志哦,不然没得点呐
private ImageView myImage1;
/**
* 下面这个两个资源,小x提示下,就是在定义的时候必须一一对应,比如:10:10
* 如果少了任何一项的话,会报数组越界异常的,所以稍微注意下
* 另外,小x的DEMO本来GridView中内容很少的,但是手闲的,试了下内容撑不下
* 一个屏幕时怎么办,没想安卓那么强大,呵,自己扩充,效果图我已经巾上面咯
*/
//声明所有图标资源
private int icons[] = {
R.drawable.angry_birds,R.drawable.browser,R.drawable.dropbox,
R.drawable.googleearth,R.drawable.lastfm,R.drawable.xiaoma,
R.drawable.xbmc,R.drawable.youtube,R.drawable.notes,
R.drawable.messages_dock,R.drawable.contacts,
R.drawable.facebook,R.drawable.wapedia
};
//声明所有图标资源title
private String items[] = {
"愤怒的小鸟","浏览器","dropbox","谷歌地球","AS","小马果的驴子","嘛东西",
"YouTuBe","记事本","消息提示","通讯薄","面谱","WAP"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
/**
* 初始化方法实现
*/
private void init() {
//我的抽屉
sDrawer = (SlidingDrawer)findViewById(R.id.mySliding);
//点击抽屉时的小图标
myImage1 = (ImageView)findViewById(R.id.myImage1);
//抽屉中要显示的内容
gvGridView = (GridView)findViewById(R.id.gridView);
//初始化自定义的网格布局适配器并设置到网络布局上
GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), items, icons);
gvGridView.setAdapter(adapter);
gvGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/**
* 此处可以实现单击不同图标时的不同功能哦,小x就不多讲废话,简单提示下
*/
Toast.makeText(getApplicationContext(), "单击了第"+position+"项", Toast.LENGTH_SHORT).show();
}
});
/**
* 下面给我的抽屉添加两个事件监听器,大家也可以只加载一个,因为考虑到用户体验
* 在点击抽屉的小标志打开抽屉时我设置一个打开的图标,关闭时设置关闭图标,这样
* 比较好玩,吼吼,下面两个监听大家随意
*/
//打开抽屉监听
sDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
myImage1.setImageResource(R.drawable.close);
}
});
//关闭抽屉监听
sDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
public void onDrawerClosed() {
myImage1.setImageResource(R.drawable.open);
}
});
}
}GridViewAdapter
package com.amaker.sliding;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* @Title: GridViewAdapter.java
* @Package com.xiaoma.www
* @Description: 自定义网络布局适配器
* @author MZH
* 老规矩,基类适配器中,小x注释就主要写下getView()方法的注释,其它的老样子
*/
public class GridViewAdapter extends BaseAdapter {
private Context con ;
private String [] itemStrings ;
private int [] icons ;
public GridViewAdapter(Context context,String [] items,int [] icons) {
this.con = context;
this.itemStrings = items;
this.icons = icons;
}
public int getCount() {
return itemStrings.length;
}
public Object getItem(int position) {
return itemStrings[position];
}
public long getItemId(int position) {
return position;
}
/**
* 这个地方我们就可以用LayoutInflate加载器来加载我们自己的布局
*/
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(con);
/**
* 这个地方小x加下注释,加深记忆,下面这个方法的两个参数作用如下:
* resource: 用DOM方式解析加载一个XML文件的所有结点及相关属性
* root:将当前自定义的布局加载到指定的已生成的父View中
* 这个方法的返回值大家注意一下:
* 如果指定的父View已经存在的话,那就把当前我们的自定义布局加载到这个父View中,
* 否则就直接加载我们当前的自定义布局文件到Activity中哦
*/
View view = (View)inflater.inflate(R.layout.grid, null);
ImageView iv =(ImageView)view.findViewById(R.id.icon);
TextView tv = (TextView)view.findViewById(R.id.text);
iv.setImageResource(icons[position]);
tv.setText(itemStrings[position]);
return view;
}
}
grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="20sp"
android:gravity="center"
android:textColor="@drawable/black"
/>
</LinearLayout>main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<!-- android:handle指定我们点击抽屉时的小图标,这个必须指定,否则没办法点,主要是去哪点出来 ?
android:content指定我们的抽屉里面加载的布局
-->
<SlidingDrawer
android:id="@+id/mySliding"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:handle="@+id/layout1"
android:content="@+id/gridView"
android:orientation="horizontal"
>
<LinearLayout
android:id="@+id/layout1"
android:layout_width="35px"
android:layout_height="fill_parent"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/myImage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="/blog_article/@drawable/open/index.html"
/>
</LinearLayout>
<GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:gravity="center"
/>
</SlidingDrawer>
</RelativeLayout>color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="darkgray">#808080</drawable> <drawable name="white">#FFFFFF</drawable> <drawable name="black">#000000</drawable> </resources>
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.sliding"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/xiaoma"
android:label="@string/app_name" >
<activity
android:name=".SlidingDrawerDemoActivityActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>最新技术文章: