原文:http://www.eoeandroid.com/thread-417-1-3.html
在原文上修改,使得Buttton不能拖动出屏幕范围,代码如下:
public class DraftTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayMetrics dm=getResources().getDisplayMetrics();
final int screenWidth=dm.widthPixels;
final int screenHeight=dm.heightPixels-50;
final Button b=(Button)findViewById(R.id.btn);
b.setOnTouchListener(new OnTouchListener(){
int lastX,lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int ea=event.getAction();
Log.i("TAG", "Touch:"+ea);
switch(ea){
case MotionEvent.ACTION_DOWN:
lastX=(int)event.getRawX();
lastY=(int)event.getRawY();
break;
/**
* layout(l,t,r,b)
* l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
* */
case MotionEvent.ACTION_MOVE:
int dx=(int)event.getRawX()-lastX;
int dy=(int)event.getRawY()-lastY;
int l=v.getLeft()+dx;
int b=v.getBottom()+dy;
int r=v.getRight()+dx;
int t=v.getTop()+dy;
if(l<0){
l=0;
r=l+v.getWidth();
}
if(t<0){
t=0;
b=t+v.getHeight();
}
if(r>screenWidth){
r=screenWidth;
l=r-v.getWidth();
}
if(b>screenHeight){
b=screenHeight;
t=b-v.getHeight();
}
v.layout(l, t, r, b);
lastX=(int)event.getRawX();
lastY=(int)event.getRawY();
Toast.makeText(DraftTest.this,
"当前位置:"+l+","+t+","+r+","+b,
Toast.LENGTH_SHORT).show();
v.postInvalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}});
}
}
运行效果如下:
源文件如附件
testDraft2.rar
(24.49 KB, 下载次数: 469)
获取已安装程序的名字、包名、版本和图标:
Java代码 1.class PInfo {
2. private String appname = "";
3. private String pname = "";
4. private String versionName = "";
5. private int versionCode = 0;
6. private Drawable icon;
7. private void prettyPrint() {
8. log(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode + "\t");
9. }
10.}
11.
12.private void listPackages() {
13. ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
14. final int max = apps.size();
15. for (int i=0; i<max; i++) {
16. apps.get(i).prettyPrint();
17. }
18.}
19.
20.private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
21. ArrayList<PInfo> res = new ArrayList<PInfo>();
22. List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
23. for(int i=0;i<packs.size();i++) {
24. PackageInfo p = packs.get(i);
25. if ((!getSysPackages) && (p.versionName == null)) {
26. continue ;
27. }
28. PInfo newInfo = new PInfo();
29. newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
30. newInfo.pname = p.packageName;
31. newInfo.versionName = p.versionName;
32. newInfo.versionCode = p.versionCode;
33. newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
34. res.add(newInfo);
35. }
36. return res;
37.}
获取未安装的APK信息:
在前面的文章发过一篇《获取已安装程序的名字、包名、版本和图标》,当时有朋友问我怎么获取未安装的APK信息(如:软件名称、包名、图标等等)。当时还不知道能读取未安装的APK信息,也没遇到这样的需求,所以也没去看怎么做。现在终于知道了,很简单。看代码会比较明了。
Java代码 1./** 获取未安装的APK信息
2. * @param context
3. * @param archiveFilePath APK文件的路径。如:/sdcard/download/XX.apk
4. */
5. public void getUninatllApkInfo(Context context, String archiveFilePath){
6. PackageManager pm = context.getPackageManager();
7. PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
8. if(info != null){
9. ApplicationInfo appInfo = info.applicationInfo;
10. String appName = pm.getApplicationLabel(appInfo).toString();
11. String packageName = appInfo.packageName;
12. Drawable icon = pm.getApplicationIcon(appInfo);
13. }
14. }
配置NDK
1、 将android-ndk
-r4b-linux-x86.zip包解压。
我是建个一个工作目录在/mnt/hgfs/VMwareShare/android-ndk
-r4/
将压缩包放在此目录下,
~$ unzip android-ndk
-r4b-linux-x86.zip
2、为ndk
-build设置环境变量
(为了方便使用而已)
修改你的.bashrc 文件
~$ vi ~/.bashrc
在最后添加
NDK=/mnt/hgfs/VMwareShare/android-ndk
-r4/
export NDK
保存退出。
让设置马上生效
~$ source ~/.bashrc
测试下samples里hello-jni的例子是否配置ok
~$ $NDK/ndk
-build -C $NDK/samples/hello-jni
打印信息:
make: Entering directory `/workspace/ndk
/ndk
r4/samples/hello-jni'
Gdbserver : [arm-eabi-4.4.0] /workspace/ndk
/ndk
r4/samples/hello-jni/libs/armeabi/gdbserver
Gdbsetup : /workspace/ndk
/ndk
r4/samples/hello-jni/libs/armeabi/gdb.setup
Gdbsetup : + source directory /workspace/ndk
/ndk
r4/samples/hello-jni/jni
Install : libhello-jni.so => /workspace/ndk
/ndk
r4/samples/hello-jni/libs/armeabi
make: Leaving directory `/workspace/ndk
/ndk
r4/samples/hello-jni'
出现以上类似信息你就ok了,在hello-jni工程的obj下有你想要的东西。
另外一种编译方法:
# cd /workspace/ndk
/ndk
r4/samples/hello-jni/
# ndk
-build
也是ok的
这里我们为ndk
-build 设置了环境变量所以能够看到这个命令。
建议:NDKROOT=/home/myleft/android-ndk
-r4b
export PATH=$NDKROOT
PATH而不是export NDKROOT