举例说明android中Bitmap、ListView以及ImageView的综合使用---显示图片并获取图片的宽和高

程序如下所示:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class A02Activity extends Activity {
 private TextView tv;//用来显示图片的宽或高
 private ImageView iv;//用来显示图片
 private static final int CONTEXT_ITEM01=Menu.FIRST;//ListView中的各个小项目
 private static final int CONTEXT_ITEM02=Menu.FIRST+1; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.tv);
        iv=(ImageView)findViewById(R.id.iv);
        //把图片添加到ImageView当中
        iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.b));
        //相当于电脑当中鼠标点击右键的功能的实现
        iv.setOnCreateContextMenuListener(new ListView.OnCreateContextMenuListener(){
   @Override
   public void onCreateContextMenu(ContextMenu menu, View v,
     ContextMenuInfo menuInfo) {
    //把两个选项添加到ImageView当中    
    menu.add(Menu.NONE, CONTEXT_ITEM01, 0, R.string.context01);
    menu.add(Menu.NONE, CONTEXT_ITEM02, 0, R.string.context02);    
   }         
        });
    }
    //用下面的方法来判断单击了哪个item,并获取图片的长或宽
    public boolean onContextItemListener(MenuItem item){
     Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.b);
     int width=b.getWidth();
     int height=b.getHeight();     
     switch(item.getItemId()){
     case CONTEXT_ITEM01:
      String s01=getResources().getString(R.string.str_width)+Integer.toBinaryString(width);
      tv.setText(s01);
      break;
     case CONTEXT_ITEM02:
      String s02=getResources().getString(R.string.str_height)+Integer.toString(height);
      tv.setText(s02);
      break;     
     }
     return super.onContextItemSelected(item);
    }
}

 

你可能感兴趣的:(android,String,ListView,Class,import,menu)