当前位置: 编程技术>移动开发
本页文章导读:
▪批改光标颜色 修改光标颜色
在使用EditText的XML 文件中加入一个属性:android:textCursorDrawable="@null"android:textCursorDrawable 这个属性是用来控制光标颜色的,"@null" 是作用是让光标颜色和text color一样android:te.........
▪ Java实现内存共享 【原创】Java实现内存共享
内存共享就是对同一段内存的读写;用来进行进程之间的通信。首先是写的代码:
package com.sharememory.test;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.n.........
▪ 仿人们网右边可推出的效果 仿人人网右边可推出的效果
看了这篇模仿facebook布局效果还有这篇仿朋友网左右两边可推出的菜单效果感觉实现太复杂,自己又写过这篇可左右两侧挤压傍边布局的Android抽屉,完全可以按.........
[1]批改光标颜色
来源: 互联网 发布时间: 2014-02-18
修改光标颜色
在使用EditText的XML 文件中加入一个属性:
android:textCursorDrawable="@null"
android:textCursorDrawable 这个属性是用来控制光标颜色的,
"@null" 是作用是让光标颜色和text color一样
android:textCursorDrawable 的用法可以查看android sdk
Reference to a drawable that will be drawn under the insertion cursor.
Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".
This corresponds to the global attribute resource symbol textCursorDrawable.
Constant Value: 70 (0x00000046)
[2] Java实现内存共享
来源: 互联网 发布时间: 2014-02-18
【原创】Java实现内存共享
内存共享就是对同一段内存的读写;用来进行进程之间的通信。
首先是写的代码:
然后是读的代码
上面代码一个先运行的时候,会上锁(文件锁),另外其他的进程来访问的时候就访问不了,通过锁的机制来达到资源互斥和同步。
内存共享就是对同一段内存的读写;用来进行进程之间的通信。
首先是写的代码:
package com.sharememory.test;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class WriteMemory {
String fileName = "shm.lock";
RandomAccessFile raFile;
FileChannel fc;
int iSize = 1024;
MappedByteBuffer mapBuf;
int iMode;
public WriteMemory() {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws Exception {
raFile = new RandomAccessFile(fileName, "rw");
fc = raFile.getChannel();
mapBuf = fc.map(FileChannel.MapMode.READ_WRITE, 0, iSize);
}
public void clearBuffer() {
// 清除文件内容
for (int i = 0; i < 1024; i++) {
mapBuf.put(i, (byte) 0);
}
}
public void putBuffer() throws Exception{
for (int i = 65; i < 91; i++) {
int index = i - 63;
int flag = mapBuf.get(0); // 可读标置第一个字节为 0
if (flag != 0) { // 不是可写标示 0,则重复循环,等待
i--;
continue;
}
mapBuf.put(0, (byte) 1); // 正在写数据,标志第一个字节为 1
mapBuf.put(1, (byte) (index)); // 写数据的位置
System.out.println("程序 WriteShareMemory:"
+ System.currentTimeMillis() + ":位置:" + index + " 写入数据:"
+ (char) i);
mapBuf.put(index, (byte) i);// index 位置写入数据
mapBuf.put(0, (byte) 2); // 置可读数据标志第一个字节为 2
Thread.sleep(513);
}
}
public boolean getLock() {
FileLock lock = null;
try {
lock = fc.tryLock();
} catch (IOException e) {
e.printStackTrace();
}
if (lock == null) {
return false;
} else {
return true;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
WriteMemory map = new WriteMemory();
if (map.getLock()) {
try {
map.putBuffer();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("can't get lock");
}
}
}
然后是读的代码
package com.sharememory.test;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class ReadMemory {
String fileName = "shm.lock";
RandomAccessFile raFile;
FileChannel fc;
int iSize = 1024;
MappedByteBuffer mapBuf;
int iMode;
int lastIndex = 0;
public ReadMemory() {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws Exception {
raFile = new RandomAccessFile(fileName, "rw");
fc = raFile.getChannel();
mapBuf = fc.map(FileChannel.MapMode.READ_WRITE, 0, iSize);
}
public void clearBuffer() {
// 清除文件内容
for (int i = 0; i < 1024; i++) {
mapBuf.put(i, (byte) 0);
}
}
public void getBuffer() throws Exception{
for(int i=1;i<27;i++){
int flag = mapBuf.get(0); //取读写数据的标志
int index = mapBuf.get(1); //读取数据的位置,2 为可读
if(flag != 2 || index == lastIndex){ //假如不可读,或未写入新数据时重复循环
i--;
continue;
}
lastIndex = index;
System.out.println("程序 ReadShareMemory:" + System.currentTimeMillis() +
":位置:" + index +" 读出数据:" + (char)mapBuf.get(index));
mapBuf.put(0,(byte)0); //置第一个字节为可读标志为 0
if(index == 27){ //读完数据后退出
break;
}
}
}
public boolean getLock() {
FileLock lock = null;
try {
lock = fc.tryLock();
} catch (IOException e) {
e.printStackTrace();
}
if (lock == null) {
return false;
} else {
return true;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ReadMemory map = new ReadMemory();
if (map.getLock()) {
try {
map.getBuffer();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("can't get lock");
}
}
}
上面代码一个先运行的时候,会上锁(文件锁),另外其他的进程来访问的时候就访问不了,通过锁的机制来达到资源互斥和同步。
[3] 仿人们网右边可推出的效果
来源: 互联网 发布时间: 2014-02-18
仿人人网右边可推出的效果
看了这篇模仿facebook布局效果还有这篇仿朋友网左右两边可推出的菜单效果
感觉实现太复杂,自己又写过这篇可左右两侧挤压傍边布局的Android抽屉,完全可以按照这个仿一个出来,简单又容易理解。
先看布局,布局很重要:
再看代码:
接口:
打开前:
打开后:
这样设计有一点不好,就是所有的代码都需要写在一个activity中,代码多了会很臃肿。
小米测试通过。
看了这篇模仿facebook布局效果还有这篇仿朋友网左右两边可推出的菜单效果
感觉实现太复杂,自己又写过这篇可左右两侧挤压傍边布局的Android抽屉,完全可以按照这个仿一个出来,简单又容易理解。
先看布局,布局很重要:
<?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"
>
<LinearLayout android:id="@+id/layout_left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<!-- 这里添加其他控件,比如ListView -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wwwwwwwwww"
android:textColor="@android:color/black"
/>
</LinearLayout>
<LinearLayout android:id="@+id/layout_right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/black"
android:layout_alignParentTop="true"
android:layout_marginLeft="280dip"
>
<ImageView android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="/blog_article/@drawable/ic_launcher/index.html"
android:contentDescription="打开/关闭"
/>
<!-- 为了不让里面的控件出现“折叠”现象,所以给了个足够长的固定宽度 -->
<!-- 实际编程中该宽度最好计算求的 -->
<LinearLayout android:id="@+id/layout_max_width"
android:layout_width="300dip"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<!-- 这里添加其他控件,比如ListView -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wwwwwwwwww"
android:textColor="@android:color/white"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wwwwwwwwww"
android:textColor="@android:color/white"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
再看代码:
package com.dl.test;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
public class App extends Activity implements OnPanelStatusChangedListener{
private boolean hasMeasured=false;
private LinearLayout layout_left;
private LinearLayout layout_right;
private ImageView iv;
private LayoutParams lp;
private int layout_left_width,layout_right_width=0;
/**每次自动展开/收缩的范围*/
private int MAX_WIDTH=0;
/**每次自动展开/收缩的速度*/
private final static int SPEED=20;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout_left=(LinearLayout)findViewById(R.id.layout_left);
layout_right=(LinearLayout)findViewById(R.id.layout_right);
iv=(ImageView)findViewById(R.id.iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutParams lp = (android.widget.RelativeLayout.LayoutParams)layout_right.getLayoutParams();
if (lp.leftMargin >=MAX_WIDTH)// CLOSE的状态
new AsynMove().execute(new Integer[] { -SPEED });// 负数展开
else if (lp.leftMargin >= 0)// OPEN的状态
new AsynMove().execute(new Integer[] { SPEED });// 正数收缩
}
});
ViewTreeObserver observer = layout_right.getViewTreeObserver();
// observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
// @Override
// public void onGlobalLayout() {
// if (hasMeasured == false){
// layout_right_width = layout_right.getMeasuredWidth();//105
// layout_left_width=layout_left.getMeasuredWidth();//480
// MAX_WIDTH=layout_left_width-layout_right_width;//375
// hasMeasured = true;
// }
// }
// });
//为了取得控件的宽
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){
public boolean onPreDraw(){
if (hasMeasured == false){
layout_right_width = layout_right.getMeasuredWidth();
layout_left_width=layout_left.getMeasuredWidth();
MAX_WIDTH=layout_left_width-layout_right_width;
// Log.i("tag", "MAX_WIDTH=="+MAX_WIDTH);
hasMeasured = true;
//设置可拉动容器的宽为全屏(即不可拉动容器)的宽
View layout_max_width=findViewById(R.id.layout_max_width);
LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)layout_max_width.getLayoutParams();
lp.width=layout_left_width;
layout_max_width.setLayoutParams(lp);
}
return true;
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
}
class AsynMove extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
int times;
if (MAX_WIDTH % Math.abs(params[0]) == 0)// 整除
times = MAX_WIDTH / Math.abs(params[0]);
else
times = MAX_WIDTH / Math.abs(params[0]) + 1;// 有余数
for (int i = 0; i < times; i++) {
publishProgress(params);
try {
Thread.sleep(Math.abs(params[0]));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... params) {
LayoutParams lp = (LayoutParams)layout_right.getLayoutParams();
if (params[0] < 0)
lp.leftMargin = Math.max(lp.leftMargin + params[0], 0);
else
lp.leftMargin = Math.min(lp.leftMargin + params[0], MAX_WIDTH);
if(lp.leftMargin<=0){//展开之后
onPanelOpened();//调用OPEN回调函数
}
else if(lp.leftMargin>=MAX_WIDTH){//收缩之后
onPanelClosed();//调用CLOSE回调函数
}
layout_right.setLayoutParams(lp);
}
}
@Override
public void onPanelOpened() {
// TODO Auto-generated method stub
Log.i("tag", "=========onPanelOpened========");
}
@Override
public void onPanelClosed() {
// TODO Auto-generated method stub
Log.i("tag", "=========onPanelClosed========");
}
}
接口:
package com.dl.test;
public interface OnPanelStatusChangedListener {
void onPanelOpened();
void onPanelClosed();
}
打开前:
打开后:
这样设计有一点不好,就是所有的代码都需要写在一个activity中,代码多了会很臃肿。
小米测试通过。
最新技术文章: