您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
79_android下的手势识别-手势持久化与手势加载到内存
发布时间:2021-03-21 10:40:38编辑:雪饮阅读()
昨天有人反映说是手势库文件在另外一个项目中读取到手势库大小为0的问题,怀疑我的代码并没有持久化到文件的原因。其实不然,持久化肯定是持久化了,那个虽然在另外一个项目中获取到手势库中的手势个数明显少于昨天操作新增的手势个数,这个个人以为应该是昨天那个项目一直修改代码,所以以最后一次代码修改的部署为准,在最后一次修改代码结束后所有操作的手势应该都会被持久化的。
那么接下来问题的主题就是如何解决在另外一个项目中获取的手势库大小为0的问题。
这里还是以建立一个新的项目来读取吧。
首先是清单文件,我们必须要有外存的读取权限
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.getgesturelistfromfile">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GetGestureListFromFile">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.getgesturelistfromfile">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GetGestureListFromFile">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
那么布局就一个按钮和一个listview,按钮负责向这个listview渲染手势库数据列表
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="查看手势列表"
android:onClick="getGestureList"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gestureList"
>
</ListView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
接下来就是实现的关键逻辑MainActivity.java:
package com.example.getgesturelistfromfile;
import androidx.appcompat.app.AppCompatActivity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private List<GestureData> gestureDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getGestureList(View view) {
GestureLibrary library= GestureLibraries.fromFile("sdcard/mygestures");
//把手势加载到内存,这里是关键,如果没有load,则直接从文件读取的手势库大小就是0
library.load();
Set<String> entries = library.getGestureEntries();
System.out.println("手势库大小:"+entries.size());
gestureDatas = new ArrayList<GestureData>();
int serialNumber=0;
for (String key : entries) {
System.out.println(key);
ArrayList<Gesture> gestures= library.getGestures(key);
for(Gesture gesture:gestures){
Bitmap bitmap=gesture.toBitmap(128,128,10,0xff0000ff);
GestureData gestureData = new GestureData();
gestureData.setBitmap(bitmap);
gestureData.setBitmapKey(key);
gestureData.setSerialNumber(serialNumber);
gestureDatas.add(gestureData);
}
}
ListView mListView = (ListView) this.findViewById(R.id.gestureList);
mListView.setAdapter(new MyAdatper());
}
public class MyAdatper extends BaseAdapter {
public int getCount() {
return gestureDatas.size();
}
public Object getItem(int position) {
return gestureDatas.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
GestureData gestureData = gestureDatas.get(position);
Bitmap bmp =gestureData.getBitmap();
imageView.setImageBitmap(bmp);
return (View)imageView;
}
}
}
那么最后就是这个关键逻辑中所依赖的一个实体类,即昨天那个bitmap中转类,不过这里由于不需要意图传递,我们直接渲染在主界面的,所以这里bitmap实体类就不需要实现序列化了。
GestureData.java:
package com.example.getgesturelistfromfile;
import android.graphics.Bitmap;
public class GestureData{
Bitmap bitmap;
Integer serialNumber;
String bitmapKey;
public Bitmap getBitmap() {
return this.bitmap;
}
public void setBitmap(Bitmap bitmap){
this.bitmap=bitmap;
}
public Integer getSerialNumber() {
return this.serialNumber;
}
public void setSerialNumber(Integer serialNumber) {
this.serialNumber = serialNumber;
}
public String getBitmapKey() {
return this.bitmapKey;
}
public void setBitmapKey(String bitmapKey) {
this.bitmapKey = bitmapKey;
}
}
那么最后可以看到这样读取是没有问题的
别忘了勾选对应读取外存权限哦
关键字词:android,手势,持久化,内存