Android navigation pointer in listview items
I am trying to make a listview with direction pointers in the list items just like gooing latitude: http://www.eurodroid.com/pics/android_google_maps_latitude_update_2.png
I already have working code, but i am worried about the performance. Everytime the orientation sensor(thats a lot) the code rebuilds the entire list with the listview adapter when i only want to rotate the direction pointer image, so it is doing to same as calling for notifyDataSetChanged.
I tried to see what happens if i only call for an setText() whenever the sensor changes, i found out that it does not rebuild the entire list then.
public class SensorWatcher{
private SensorManager mgr = null;
public float azimuth = 0;
private Main main;
SensorWatcher(Main mainContext){
main = mainContext;
mgr = (SensorManager)mainContext.getSystemService(Context.SENSOR_SERVICE);
mgr.registerListener(listener, mgr.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
}
private SensorEventListener listener = new SensorEventListener() {
int newAzimuth;
public void onSensorChanged(Sensor开发者_StackOverflowEvent e) {
if (e.sensor.getType()==Sensor.TYPE_ORIENTATION) {
newAzimuth = Math.round(e.values[0]);
if(newAzimuth != azimuth){
azimuth= newAzimuth;
updateItems();
//main.adapter.notifyDataSetChanged();
}
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
};
public void updateItems(){
ListView list = main.list;
ImageView img;
View listItem;
if(list != null){
for(int i =list.getFirstVisiblePosition();i<=list.getLastVisiblePosition();i++ ){
if(i< list.getChildCount()){
listItem=(View) list.getChildAt(i);
//listItem = list.getAdapter().getView(i, null, null);
img =(ImageView) listItem.findViewById(R.id.allowtest);
Bitmap bmp = BitmapFactory.decodeResource(main.getResources(), R.drawable.arrowup);
Matrix mtx = new Matrix();
mtx.postRotate(calculateDirection(list,i));
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
img.setImageDrawable(bmd);
}
}
}
}
For making changes in the list u need to call notifyDataSetChanged.. There is no other alternative for that .
精彩评论