android一些基础功能汇总
1.Android开发给图片加边框
http://www.android100.org/html/201303/20/1941.html
2.Android开发之Bitmap图片旋转
http://www.android100.org/html/201303/20/1934.html
3.安卓源码服务专家
http://www.javaapk.com/
http://www.javaapk.com/source/category/demo
4.ImageLoader显示图片
当然可以从sdcard中加载图片。他可以接收的URL有以下几种: String imageUri = "http://site.com/image.png"; // from Web String imageUri = "file:///mnt/sdcard/image.png"; // from SD card String imageUri = "content://media/external/audio/albumart/13"; // from content provider String imageUri = "assets://image.png"; // from assets String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
4.屏幕长亮
方法1:使用PowerManager和WakeLock
InAndroidManifest.xml:
1
<uses-permission android:name="android.permission.WAKE_LOCK" /> 在你的activity中: PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); // 在 onResume()方法中加入: mWakeLock.acquire(); //锁住屏幕,使其常亮 //在 onPause() 中加入: mWakeLock.release();//解锁屏幕。让系统管理屏幕
方法2:使用标志windowflagFLAG_KEEP_SCREEN_ON
@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }
5.Math取整方法
舍掉小数取整:Math.floor(2)=2 舍掉小数取整:Math.floor(2.1)=2 舍掉小数取整:Math.floor(-2.1)=-3 舍掉小数取整:Math.floor(-2.5)=-3 舍掉小数取整:Math.floor(-2.9)=-3 四舍五入取整:Math.rint(2)=2 四舍五入取整:Math.rint(2.1)=2 四舍五入取整:Math.rint(-2.5)=-2 四舍五入取整:Math.rint(2.5)=2 四舍五入取整:Math.rint(2.9)=3 四舍五入取整:Math.rint(-2.9)=-3 四舍五入取整:Math.rint(-2.49)=-2 四舍五入取整:Math.rint(-2.51)=-3 凑整:Math.ceil(2)=2 凑整:Math.ceil(2.1)=3 凑整:Math.ceil(2.5)=3 凑整:Math.ceil(2.9)=3 舍掉小数取整:Math.floor(-2)=-2 舍掉小数取整:Math.floor(-2.1)=-3 舍掉小数取整:Math.floor(-2.5)=-3 舍掉小数取整:Math.floor(-2.9)=-3 凑整:Math.ceil(-2)=-2 凑整:Math.ceil(-2.1)=-2 凑整:Math.ceil(-2.5)=-2 凑整:Math.ceil(-2.9)=-2 Math.round(3.14)3 Math.round(3.5)4 Math.round(-3.14)-3 Math.round(-3.5)-3
总结:floor向下取整,ceil向上取整;round和rint四舍五入,取绝对值后舍入,然后加上符号,遇到.5的时候向绝对值小的方向舍之。
6.ImageView图片显示
//Drawable 显示SD卡里图片 Drawable drawable = Drawable .createFromPath(ConstantEp.path_reading01_images + path); System.out.println("drawable :"+drawable); ivPic.setImageDrawable(drawable); //Bitmap 显示SD卡里图片 Bitmap b = ImageCacheUtil.getResizedBitmap(null, null, mContext, ImageCacheUtil.readSdPic(ConstantEp.path_reading01_images + path), 80, false); ivPic.setImageBitmap(b); //反射 显示图片 int id = BaseCommon.getImage(mContext, path.substring(0, path.length()-4)); System.out.println("id :"+id); ivPic.setImageResource(id);
//ImageCacheUtil
import java.io.File; import java.io.InputStream; import android.content.ContentResolver; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.net.Uri; import android.os.Environment; /** * @author frankiewei. 工具类. */ public class ImageCacheUtil { /** * 获取合适的Bitmap平时获取Bitmap就用这个方法吧. * * @param path * 路径. * @param data * byte[]数组. * @param context * 上下文 * @param uri * uri * @param target * 模板宽或者高的大小. * @param width * 是否是宽度 * @return */ public static Bitmap getResizedBitmap(String path, byte[] data, Context context, Uri uri, int target, boolean width) { Options options = null; if (target > 0) { /*Options info = new Options(); // 这里设置true的时候,decode时候Bitmap返回的为空, // 将图片宽高读取放在Options里. info.inJustDecodeBounds = false; info.inPreferredConfig = Bitmap.Config.RGB_565; decode(path, data, context, uri, info); int dim = info.outWidth; if (!width) dim = Math.max(dim, info.outHeight); int ssize = sampleSize(dim, target);*/ int ssize = 1; options = new Options(); options.inPreferredConfig=Bitmap.Config.RGB_565;//表示16位位图 565代表对应三原色占的位数 options.inInputShareable=true; options.inPurgeable=true;//设置图片可以被回收 options.inSampleSize = ssize; } Bitmap bm = null; try { bm = decode(path, data, context, uri, options); } catch (Exception e) { e.printStackTrace(); } return bm; } /** * 解析Bitmap的公用方法. * * @param path * @param data * @param context * @param uri * @param options * @return */ public static Bitmap decode(String path, byte[] data, Context context, Uri uri, BitmapFactory.Options options) { Bitmap result = null; if (path != null) { result = BitmapFactory.decodeFile(path, options); } else if (data != null) { result = BitmapFactory.decodeByteArray(data, 0, data.length, options); } else if (uri != null) { // uri不为空的时候context也不要为空. ContentResolver cr = context.getContentResolver(); InputStream inputStream = null; try { inputStream = cr.openInputStream(uri); result = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); } } return result; } /** * 获取合适的sampleSize. 这里就简单实现都是2的倍数啦. * * @param width * @param target * @return */ private static int sampleSize(int width, int target) { int result = 1; for (int i = 0; i < 10; i++) { if (width < target * 2) { break; } width = width / 2; result = result * 2; } return result; } /** * 读取本地图片 * @param context * @param id=等价=R.drawable.pic * @return */ public Bitmap recodeResPic(Context context , int id, int target) { Resources r = context.getResources(); Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + r.getResourcePackageName(id) + "/" + r.getResourceTypeName(id) + "/" + r.getResourceEntryName(id)); Bitmap bitmap = ImageCacheUtil.getResizedBitmap(null, null, context, uri, target, false); // Bitmap bitmap = ImageCacheUtil.getResizedBitmap(null, null, // context, uri, 400, false); return bitmap; } // 本地sd卡里的图片读取转换成URI public static Uri readSdPic(String path) { File file = new File(path); return Uri.fromFile(file); } public Bitmap recodeResPic(Context context , int id) { Resources r = context.getResources(); Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + r.getResourcePackageName(id) + "/" + r.getResourceTypeName(id) + "/" + r.getResourceEntryName(id)); Bitmap bitmap = ImageCacheUtil.getResizedBitmap(null, null, context, uri, 100, false); return bitmap; } }
7.ImageView
ImageViewandroid:scaleType="centerCrop"
CENTER:在视图中使图像居中,不执行缩放。
CENTER_CROP:均衡的缩放图像(保持图像原始比例),使图片的两个坐标(宽、高)都大于等于相应的视图坐标(负的内边距)
CENTER_INSIDE:均衡的缩放图像(保持图像原始比例),使图片的两个坐标(宽、高)都小于等于相应的视图坐标(负的内边距)
FIT_CENTER:使用CENTER方式缩放图像
FIT_END:使用END方式缩放图像
FIT_START:使用START方式缩放图像
FIT_XY:使用FILL方式缩放图像
MATRIX:绘制时,使用图像矩阵方式缩放
8.实现水平ListView,并且解决水平ListView在ScrollView中出现的滑动冲突
链接:http://liucanwen.iteye.com/blog/2020004
9.获得控件的宽高
// 获取控件的宽高 private int getViewWidth(View view) { int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(w, h); int height = view.getMeasuredHeight(); int width = view.getMeasuredWidth(); System.out.println("measure width=" + width + " height=" + height); return width; }
显示本地图片报错:OOM
[size=medium]public Bitmap readBitmap(Context context, int id){ BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig=Bitmap.Config.RGB_565;//表示16位位图 565代表对应三原色占的位数 opt.inInputShareable=true; opt.inPurgeable=true;//设置图片可以被回收 InputStream is = context.getResources().openRawResource(id); return BitmapFactory.decodeStream(is, null, opt); } private AnimationDrawable setData(Context context) { AnimationDrawable frameAnim = new AnimationDrawable(); for (int i = 0; i <10; i++) { int resId = new BaseCommon().getImageId(context, ("star_" + i)); Bitmap bmp = readBitmap(mContext, resId); BitmapDrawable bmpDraw = new BitmapDrawable(bmp); frameAnim.addFrame(bmpDraw, 200); } // 设置为循环播放 frameAnim.setOneShot(true); // 设置ImageView的背景为AnimationDrawable return frameAnim; }[/size]