Android开发小技巧总结

jopen 13年前
     总结下一些相关技巧。    <p><b>1 </b><b>全屏</b></p>    <p align="left"></p>    <pre class="brush:java; toolbar: true; auto-links: false;">super.onCreate(savedInstanceState);        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  requestWindowFeature(Window.FEATURE_NO_TITLE);  setContentView(R.layout.main);</pre>    <p></p>    <p><b>2 </b><b>取得屏幕像素</b></p>    <p align="left"></p>    <pre class="brush:java; toolbar: true; auto-links: false;">DisplayMetrics dm=new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(dm);</pre>    <p></p>    <p><b>3 </b><b>简单拍照</b></p>    <p align="left"></p>    <pre class="brush:java; toolbar: true; auto-links: false;">button.setOnClickListener(new OnClickListener(){                    @Override             public void onClick(View arg0) {                         Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");      startActivityForResult(intent,SELECT_PICTURE_FROM_CAMERA);              }          });  //然后重载startActivityForResult添加自己的实现  @Override      protected void onActivityResult(int requestCode, int resultCode, Intent data) {      if( resultCode==RESULT_OK)      {          if(requestCode==SELECT_PICTURE_FROM_CAMERA)          {              File image=new File(Environment.getExternalStorageDirectory(), "camera.jpg");              selectedImagePath=image.getAbsolutePath();              Drawable d=Drawable.createFromPath(selectedImagePath);              ImageView.setImageDrawable(d);          }      }  } </pre>    <p></p>    <p><b>4 </b><b>从</b><b>android</b><b>本地多媒体图片文件夹中选择图片</b></p>    <p align="left"></p>    <pre class="brush:java; toolbar: true; auto-links: false;">Intent i=new Intent(Intent.ACTION_GET_CONTENT);                i.setType("image/*");                Intent wrapi=Intent.createChooser(i, "choose");                startActivityForResult(wrapi, SELECT_PICTURE_FROM_FILE);  //然后重载startActivityForResult添加自己的实现  @Override      protected void onActivityResult(int requestCode, int resultCode, Intent data) {      if( resultCode==RESULT_OK)      {          if(requestCode==SELECT_PICTURE_FROM_FILE )          {          Uri selectedImageUri = data.getData();          selectedImagePath=getPath(selectedImageUri);          imageView.setImageURI(selectedImageUri);          }      }  }   //获取当前选择的图片,并显示在imageView上      private String getPath(Uri uri)      {      String[] projection={MediaStore.Images.Media.DATA};      Cursor cursor=managedQuery(uri,projection,null,null,null);      int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);      cursor.moveToFirst();      return cursor.getString(column_index);  }     //Query实现从图片数据库中查询到所要记录</pre>    <p></p>    <p><b>5 Activity</b><b>之间带内容的跳转</b></p>    <p align="left"></p>    <pre class="brush:java; toolbar: true; auto-links: false;">Intent intent = new Intent();                    Bundle imageMsg = new Bundle();                    imageMsg.putString("filePath", selectedImagePath); /               intent.putExtras(imageMsg);                    intent.setClass(ActivityA.this,ActivityB.class);                    startActivity(intent);                    ActivityA.this.finish();</pre>    <p></p>    <p><b>6 </b><b>位图缩放</b></p>    <p align="left"><b>public</b> Bitmap zoomImage(Bitmap bgimage, <b>int</b> newWidth, <b>int</b> newHeight) {    </p>    <p align="left">       <b>int</b> width = bgimage.getWidth();</p>    <p align="left">       <b>int</b> height = bgimage.getHeight();</p>    <p align="left"> </p>    <p align="left">       Matrix matrix = <b>new</b> Matrix();</p>    <p align="left"> </p>    <p align="left">       <b>float</b> scaleWidth = ((<b>float</b>) newWidth) / width;</p>    <p align="left">       <b>float</b> scaleHeight = ((<b>float</b>) newHeight) / height;</p>    <p align="left"> </p>    <p align="left">       matrix.postScale(scaleWidth, scaleHeight);</p>    <p align="left">       Bitmap bitmap = Bitmap.<i>createBitmap</i>(bgimage, 0, 0, width, height,</p>    <p align="left">       matrix, <b>true</b>);</p>    <p align="left">       <b>return</b> bitmap;</p>    <p align="left"> </p>    <p>       }</p>    <p><b>7 Gallery</b><b>的使用</b></p>    <p>Gallery gallery=(Gallery)findViewById(R.id.<i>gallery</i>);</p>    <p align="left">        myImageAdapter imageadapter=<b>new</b> myImageAdapter(<b>this</b>); //传入context</p>    <p align="left">        gallery.setAdapter(imageadapter);</p>    <p align="left">        gallery.setOnItemClickListener(<b>new</b> Gallery.OnItemClickListener(){</p>    <p align="left">           @Override</p>    <p align="left">           <b>public</b> <b>void</b> onItemClick(AdapterView<?> arg0, View arg1, <b>int</b> arg2,<b>long</b> arg3) {</p>    <p align="left">              Bitmap selectedBitmap=BitmapFactory.<i>decodeResource</i>(getResources(),imageadapter.myImageIds[arg2]);</p>    <p align="left">           }</p>    <p align="left">        });</p>    <p>        gallery.setSelection(1);</p>    <p>//adapter的类定义</p>    <p align="left"><b>public</b> <b>class</b> myImageAdapter <b>extends</b> BaseAdapter{</p>    <p align="left"> </p>    <p align="left">    <b>int</b> mGalleryItemBackground;</p>    <p align="left">    <b>private</b> Context context;</p>    <p align="left">    <b>public</b> Integer[] myImageIds={</p>    <p align="left">           //图片的资源id号</p>    <p align="left">    };</p>    <p align="left">    <b>public</b> myImageAdapter(Context context)</p>    <p align="left">    {</p>    <p align="left">       <b>this</b>.context=context;</p>    <p align="left">       TypedArray typed_array=context.obtainStyledAttributes(R.styleable.<i>Gallery</i>);</p>    <p align="left">       </p>    <p align="left">    mGalleryItemBackground=typed_array.getResourceId(R.styleable.<i>Gallery_android_galleryItemBackground</i>   , 0);</p>    <p align="left">       //mGalleryItemBackground++;</p>    <p align="left">       typed_array.recycle();</p>    <p align="left">    }</p>    <p align="left">    @Override</p>    <p align="left">    <b>public</b> <b>int</b> getCount() {</p>    <p align="left">       // <b>TODO</b> Auto-generated method stub</p>    <p align="left">       <b>return</b> myImageIds.length;</p>    <p align="left">    }</p>    <p align="left"> </p>    <p align="left">    @Override</p>    <p align="left">    <b>public</b> Object getItem(<b>int</b> arg0) {</p>    <p align="left">       // <b>TODO</b> Auto-generated method stub</p>    <p align="left">       <b>return</b> arg0;</p>    <p align="left">    }</p>    <p align="left"> </p>    <p align="left">    @Override</p>    <p align="left">    <b>public</b> <b>long</b> getItemId(<b>int</b> arg0) {</p>    <p align="left">       // <b>TODO</b> Auto-generated method stub</p>    <p align="left">       <b>return</b> arg0;</p>    <p align="left">    }</p>    <p align="left"> </p>    <p align="left">    @Override</p>    <p align="left">    <b>public</b> View getView(<b>int</b> arg0, View arg1, ViewGroup arg2) {</p>    <p align="left">       // <b>TODO</b> Auto-generated method stub</p>    <p align="left">       ImageView imageView=<b>new</b> ImageView(context);</p>    <p align="left">       imageView.setImageResource(myImageIds[arg0]);</p>    <p align="left">       </p>    <p align="left">       imageView.setScaleType(ImageView.ScaleType.<i>FIT_XY</i>);</p>    <p align="left">       imageView.setLayoutParams(<b>new</b> Gallery.LayoutParams(128,128));</p>    <p align="left">       imageView.setBackgroundResource(mGalleryItemBackground);</p>    <p align="left">       <b>return</b> imageView;</p>    <p align="left">    }</p>    <p align="left"> </p>    <p>}</p>    <p><b>8 activity</b><b>中使用线程</b></p>    <p>private Handler mHandler=new Handler();</p>    <p align="left"><b>private</b> Runnable mRunnable=<b>new</b> Runnable()</p>    <p align="left">    {</p>    <p align="left">       @Override</p>    <p align="left">       <b>public</b> <b>void</b> run() {</p>    <p align="left">           // <b>TODO</b> Auto-generated method stub</p>    <p align="left">       }</p>    <p>};</p>    <p>mHandler.post(mRunnable);              //启动线程</p>    <p><b>9 </b><b>保存到</b><b>android</b><b>多媒体图片文件夹</b></p>    <p>android.provider.MediaStore.Images.Media.<i>insertImage</i>(getContentResolver(),resultBitmap, "picName", "descrition");</p>    <p><b>10 </b><b>其他技巧摘录自网络,见链接</b></p>    <p>1 <a href="/misc/goto?guid=4959517936175517299">android开发环境安装</a></p>    <p>2 Android模拟器中使用sdcard</p>    <p><a href="/misc/goto?guid=4959517936252758589">http://blog.csdn.net/unix_fans/archive/2009/08/20/4465038.aspx</a></p>    <p><a href="/misc/goto?guid=4959517936329168599">http://blog.csdn.net/jinlking/archive/2009/03/03/3953319.aspx</a></p>    <p>3 <b><a href="/misc/goto?guid=4959517936413501041">Android Intent 大全</a></b></p>    <p>在Android中调用图片视频、音频、录音、拍照:</p>    <p><a href="/misc/goto?guid=4959517936499357407">http://blog.csdn.net/bdwgbdwg/archive/2010/11/23/6029208.aspx</a></p>    <p>4 <a href="/misc/goto?guid=4959517936575807252">Android.Content.Provider.rar</a></p>    <p>5 <a href="/misc/goto?guid=4959517936662943951">Android中三种使用线程的方法.rar</a></p>    <p><strong>11 Activity之间动态跳转(摘自网络)</strong></p>    <p>//页面跳转<br />      int  version = Integer.valueOf(android.os.Build.VERSION.SDK);      <br />      if (version  >=  5 ) {      <br />         overridePendingTransition(R.anim.zoomin, R.anim.zoomout);  //此为自定义的动画效果,下面两个为系统的动画效果    <br />         //overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);      <br />         //overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);    <br />      }     <br />      //页面跳转</p>    <p>     //zoomin.xml</p>    <p><?xml version="1.0" encoding="utf-8"?><br /> <set  xmlns:android="<a href="/misc/goto?guid=4959517936744177530">http://schemas.android.com/apk/res/android</a>"    <br />         android:interpolator="@android:anim/decelerate_interpolator" >   <br />     <scale android:fromXScale="2.0"  android:toXScale= "1.0"    <br />            android:fromYScale="2.0"  android:toYScale= "1.0"    <br />            android:pivotX="50%p"  android:pivotY= "50%p"    <br />            android:duration="@android:integer/config_mediumAnimTime"  /><br /> </set><br /> //zoomout.xml</p>    <p><?xml version="1.0" encoding="utf-8"?><br /> <set xmlns:android="<a href="/misc/goto?guid=4959517936744177530">http://schemas.android.com/apk/res/android</a>"    <br />         android:interpolator="@android:anim/decelerate_interpolator"    <br />         android:zAdjustment="top" >   <br />     <scale android:fromXScale="1.0"  android:toXScale= ".5"    <br />            android:fromYScale="1.0"  android:toYScale= ".5"    <br />            android:pivotX="50%p"  android:pivotY= "50%p"    <br />            android:duration="@android:integer/config_mediumAnimTime"  />   <br />     <alpha android:fromAlpha="1.0"  android:toAlpha= "0"    <br />            android:duration="@android:integer/config_mediumAnimTime" /> <br /> </set></p>    <p><strong>12 模拟按钮press和release的效果</strong></p>    <p><a href="/misc/goto?guid=4959517936834912022">http://blog.csdn.net/sytzz/archive/2010/06/16/5673662.aspx</a></p>    <p><strong>13 实现画出一个透明的点</strong></p>    <p>  Paint paint=new Paint();</p>    <p>  paint.setColor(Color.BLUE); <br />   paint.setAlpha(0);  </p>    <p>  canvas.drawPoint(x, y, paint);</p>    <p>  这样就好了吗? no,你需要把你的canvas建立在一个允许透明的bitmap上,如下</p>    <p>  blankBitmap=Bitmap.createBitmap(picWidth, picHeight, Config.ARGB_8888);//不是Config.sRGB_565<br />   Canvas c = new Canvas(blankBitmap); </p>    <br /> 作者:    <a href="/misc/goto?guid=4959517936907596573" target="_blank">Aga.J</a>    <br /> 出处:    <a href="/misc/goto?guid=4959517935595607054" target="_blank">http://www.cnblogs.com/aga-j</a>