android开发小技巧笔记
2011-02-07 15:31 Aga.J 阅读(1262) 评论(0) 编辑 收藏 举报最近android大热(-0 - 也不是最近的事了。。),放假闲着没事就在android平台上做了个应用,总结下一些相关技巧。
1 全屏
super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
2 取得屏幕像素
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
3 简单拍照
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);
}
}
}
4 从android本地多媒体图片文件夹中选择图片
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实现从图片数据库中查询到所要记录
5 Activity之间带内容的跳转
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();
6 位图缩放
public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {
int width = bgimage.getWidth();
int height = bgimage.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
matrix, true);
return bitmap;
}
7 Gallery的使用
Gallery gallery=(Gallery)findViewById(R.id.gallery);
myImageAdapter imageadapter=new myImageAdapter(this); //传入context
gallery.setAdapter(imageadapter);
gallery.setOnItemClickListener(new Gallery.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Bitmap selectedBitmap=BitmapFactory.decodeResource(getResources(),imageadapter.myImageIds[arg2]);
}
});
gallery.setSelection(1);
//adapter的类定义
public class myImageAdapter extends BaseAdapter{
int mGalleryItemBackground;
private Context context;
public Integer[] myImageIds={
//图片的资源id号
};
public myImageAdapter(Context context)
{
this.context=context;
TypedArray typed_array=context.obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground=typed_array.getResourceId(R.styleable.Gallery_android_galleryItemBackground , 0);
//mGalleryItemBackground++;
typed_array.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myImageIds.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView=new ImageView(context);
imageView.setImageResource(myImageIds[arg0]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(128,128));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
8 activity中使用线程
private Handler mHandler=new Handler();
private Runnable mRunnable=new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
}
};
mHandler.post(mRunnable); //启动线程
9 保存到android多媒体图片文件夹
android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),resultBitmap, "picName", "descrition");
10 其他技巧摘录自网络,见链接
2 Android模拟器中使用sdcard
http://blog.csdn.net/unix_fans/archive/2009/08/20/4465038.aspx
http://blog.csdn.net/jinlking/archive/2009/03/03/3953319.aspx
在Android中调用图片视频、音频、录音、拍照:
http://blog.csdn.net/bdwgbdwg/archive/2010/11/23/6029208.aspx
4 Android.Content.Provider.rar
11 Activity之间动态跳转(摘自网络)
//页面跳转
int version = Integer.valueOf(android.os.Build.VERSION.SDK);
if (version >= 5 ) {
overridePendingTransition(R.anim.zoomin, R.anim.zoomout); //此为自定义的动画效果,下面两个为系统的动画效果
//overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
//overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
}
//页面跳转
//zoomin.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator" >
<scale android:fromXScale="2.0" android:toXScale= "1.0"
android:fromYScale="2.0" android:toYScale= "1.0"
android:pivotX="50%p" android:pivotY= "50%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
//zoomout.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top" >
<scale android:fromXScale="1.0" android:toXScale= ".5"
android:fromYScale="1.0" android:toYScale= ".5"
android:pivotX="50%p" android:pivotY= "50%p"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha android:fromAlpha="1.0" android:toAlpha= "0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
12 模拟按钮press和release的效果
http://blog.csdn.net/sytzz/archive/2010/06/16/5673662.aspx
13 实现画出一个透明的点
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setAlpha(0);
canvas.drawPoint(x, y, paint);
这样就好了吗? no,你需要把你的canvas建立在一个允许透明的bitmap上,如下
blankBitmap=Bitmap.createBitmap(picWidth, picHeight, Config.ARGB_8888);//不是Config.sRGB_565
Canvas c = new Canvas(blankBitmap);
作者:Aga.J
出处:http://www.cnblogs.com/aga-j
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
个人学习笔记仅供本人记录知识所用,不属发表性文章。