http://www.cnblogs.com/over140/category/253648.html

Android 特殊用法

1.让一个图片透明:

Java代码
  1. Bitmapbuffer=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_4444);buffer.eraseColor(Color.TRANSPARENT);

2.直接发送邮件:

Java代码
  1. Intentintent=newIntent(Intent.ACTION_SENDTO,Uri.fromParts("mailto","test@test.com",null));
  2. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  3. context.startActivity(intent);

3.程序控制屏幕变亮:

Java代码
  1. WindowManager.LayoutParamslp=getWindow().getAttributes();
  2. lp.screenBrightness=100/100.0f;
  3. getWindow().setAttributes(lp);

4.过滤特定文本

Java代码
  1. Filterfilter=myAdapter.getFilter();
  2. filter.filter(mySearchText);

5.scrollView scroll停止事件

Java代码
  1. setOnScrollListener(newOnScrollListener(){
  2. publicvoidonScroll(AbsListViewview,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount){
  3. //TODOAuto-generatedmethodstub}
  4. publicvoidonScrollStateChanged(AbsListViewview,intscrollState){
  5. //TODOAuto-generatedmethodstub
  6. if(scrollState==0)Log.i("a","scrollingstopped...");}});}

6. 对于特定的程序 发起一个关联供打开

C/C++代码
  1. Bitmapbmp=getImageBitmap(jpg);
  2. Stringpath=getFilesDir().getAbsolutePath()+"/test.png";
  3. Filefile=newFile(path);
  4. FileOutputStreamfos=newFileOutputStream(file);
  5. bmp.compress(CompressFormat.PNG,100,fos);
  6. fos.close();
  7. Intentintent=newIntent();
  8. intent.setAction(android.content.Intent.ACTION_VIEW);
  9. intent.setDataAndType(Uri.fromFile(newFile(path)),"image/png");
  10. startActivity(intent);
  11. 对于图片上边的不适用索引格式会出错。
  12. Intentintent=newIntent();
  13. intent.setAction(android.content.Intent.ACTION_VIEW);
  14. Filefile=newFile("/sdcard/test.mp4");
  15. intent.setDataAndType(Uri.fromFile(file),"video/*");
  16. startActivity(intent);
  17. Intentintent=newIntent();
  18. intent.setAction(android.content.Intent.ACTION_VIEW);
  19. Filefile=newFile("/sdcard/test.mp3");
  20. intent.setDataAndType(Uri.fromFile(file),"audio/*");
  21. startActivity(intent);

7.设置文本外观

Java代码
  1. setTextAppearance(context,android.R.style.TextAppearance_Medium);
  2. android:textAppearance="?android:attr/textAppearanceMedium"

8.设置单独的发起模式:

Java代码
  1. <activity
  2. android:name=".ArtistActivity"
  3. android:label="Artist"
  4. android:launchMode="singleTop">
  5. </activity>
  6. Intenti=newIntent();
  7. i.putExtra(EXTRA_KEY_ARTIST,id);
  8. i.setClass(this,ArtistActivity.class);
  9. i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  10. startActivity(i);

9.创建一个圆角图片
这个的主要原理其实就是利用遮罩,先创建一个圆角方框 然后将图片放在下面:

Java代码
  1. BitmapmyCoolBitmap=...;
  2. intw=myCoolBitmap.getWidth(),h=myCoolBitmap.getHeight();
  3. Bitmaprounder=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
  4. Canvascanvas=newCanvas(rounder);
  5. PaintxferPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
  6. xferPaint.setColor(Color.RED);
  7. canvas.drawRoundRect(newRectF(0,0,w,h),20.0f,20.0f,xferPaint);
  8. xferPaint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.DST_IN));
Java代码
  1. //然后呢实现
  2. canvas.drawBitmap(myCoolBitmap,0,0,null);
  3. canvas.drawBitmap(rounder,0,0,xferPaint);

10.在notification上的icon上加上数字 给人提示有多少个未读

Java代码
  1. Notificationnotification=newNotification(icon,tickerText,when);
  2. notification.number=4;

11背景渐变:
首先建立文件drawable/shape.xml

Java代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
  3. <gradientandroid:startColor="#FFFFFFFF"android:endColor="#FFFF0000"
  4. android:angle="270"/>
  5. </shape>

在该文件中设置渐变的开始颜色(startColor)、结束颜色(endColor)和角度(angle)

接着创建一个主题values/style.xml

Java代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stylename="NewTheme"parent="android:Theme">
  4. <itemname="android:background">@drawable/shape</item>
  5. </style>
  6. </resources>

然后在AndroidManifest.xml文件中的application或activity中引入该主题,如:

Java代码
  1. <activityandroid:name=".ShapeDemo"android:theme="@style/NewTheme">

该方法同样适用于控件 http://17f8.cn/trackback.php?tbID=259&extra=9d45e9

12. 储存数据 当你在一个实例中保存静态数据,此示例关闭后 下一个实例想引用 静态数据就会为null,这里呢必须重写applition

Java代码
  1. publicclassMyApplicationextendsApplication{
  2. privateStringthing=null;
  3. publicStringgetThing(){
  4. returnthing;
  5. }
  6. publicvoidsetThing(Stringthing){
  7. this.thing=thing;}
  8. }
  9. publicclassMyActivityextendsActivity{
  10. privateMyApplicationapp;
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. app=((MyApplication)getApplication());
  14. Stringthing=app.getThing();
  15. }
  16. }

更多相关文章

  1. 【翻译】(25)ANDROID ATOMICS OPERATIONS
  2. 2013.12.23 (2)——— android 代码调用shell
  3. Android声音播放实例代码
  4. android 静默安装 卸载 资料汇总
  5. Android(安卓)2.3的camera的虚拟对焦的去除
  6. Android(安卓)轻松实现语音识别的完整代码
  7. 代码中如何设置TextView为不可见
  8. Android(安卓)轻松实现语音识别的完整代码
  9. Android桌面小部件实例 桌面小时钟

随机推荐

  1. 当Google忙着忽悠Android的时候,各位别忘
  2. Android中shape的使用
  3. Android(安卓)中文 API――android.widge
  4. android 自定义组件 :对VelocityTracker的
  5. android中控制EditText不可编辑的问题
  6. android基础入门(二)――创建android工程
  7. 2010-11-4
  8. Android架构师之路
  9. 如何退出Android应用程序
  10. Android(安卓)之 复习大纲