欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实用编程技巧代码总结

程序员文章站 2024-03-04 16:58:53
本文实例总结了android实用编程技巧。分享给大家供大家参考,具体如下: 1.让一个图片透明: bitmap buffer = bitmap.createbi...

本文实例总结了android实用编程技巧。分享给大家供大家参考,具体如下:

1.让一个图片透明:

bitmap buffer = bitmap.createbitmap(width, height, bitmap.config.argb_4444);
buffer.erasecolor(color.transparent);

2.直接发送邮件:

intent intent = new intent(intent.action_sendto, uri .fromparts("mailto", "test@test.com", null));
intent.setflags(intent.flag_activity_new_task);
context.startactivity(intent);

3.程序控制屏幕变亮:

windowmanager.layoutparams lp = getwindow().getattributes();
lp.screenbrightness = 100 / 100.0f;
getwindow().setattributes(lp);

4.过滤特定文本

filter filter = myadapter.getfilter();
filter.filter(mysearchtext);

5.scrollview scroll停止事件

setonscrolllistener(new onscrolllistener(){
public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
// todo auto-generated method stub  }
public void onscrollstatechanged(abslistview view, int scrollstate) {
// todo auto-generated method stub
if(scrollstate == 0) log.i("a", "scrolling stopped...");  } });}

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

bitmap bmp = getimagebitmap(jpg);
string path = getfilesdir().getabsolutepath() + "/test.png";
file file = new file(path);
fileoutputstream fos = new fileoutputstream(file);
bmp.compress( compressformat.png, 100, fos );
 fos.close();
  intent intent = new intent();
  intent.setaction(android .content.intent.action_view);
  intent.setdataandtype(uri .fromfile(new file(path)), "image/png");
  startactivity(intent);

对于图片上边的不适用索引格式会出错。

intent intent = new intent();
intent.setaction(android .content.intent.action_view);
file file = new file("/sdcard/test.mp4");
intent.setdataandtype(uri .fromfile(file), "video/*");
startactivity(intent);
intent intent = new intent();
intent.setaction(android .content.intent.action_view);
file file = new file("/sdcard/test.mp3");
intent.setdataandtype(uri .fromfile(file), "audio/*");
startactivity(intent);

7.设置文本外观

settextappearance(context, android .r.style.textappearance_medium);
android :textappearance="?android :attr/textappearancemedium"

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

<activity
 android :name=".artistactivity"
 android :label="artist"
 android :launchmode="singletop">
</activity>

intent i = new intent();
i.putextra(extra_key_artist, id);
i.setclass(this, artistactivity.class);
i.addflags(intent.flag_activity_single_top);
startactivity(i);

9.创建一个圆角图片

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

bitmap mycoolbitmap = ... ;
   int w = mycoolbitmap.getwidth(), h = mycoolbitmap.getheight();
   bitmap rounder = bitmap.createbitmap(w,h,bitmap.config.argb_8888);
   canvas canvas = new canvas(rounder);
   paint xferpaint = new paint(paint.anti_alias_flag);
   xferpaint.setcolor(color.red);
   canvas.drawroundrect(new rectf(0,0,w,h), 20.0f, 20.0f, xferpaint);
   xferpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_in));
//然后呢实现
canvas.drawbitmap(mycoolbitmap, 0,0, null);
canvas.drawbitmap(rounder, 0, 0, xferpaint);

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

notification notification = new notification (icon, tickertext, when);
notification .number = 4;

11.背景渐变:

首先建立文件drawable/shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android ="http://schemas.android .com/apk/res/android " android :shape="rectangle">
  <gradient android :startcolor="#ffffffff" android :endcolor="#ffff0000"
      android :angle="270"/>
</shape>

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

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="newtheme" parent="android :theme">
<item name="android :background">@drawable/shape</item>
</style>
</resources>

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

<activity android :name=".shapedemo" android :theme="@style/newtheme">

该方法同样适用于控件

<?php xml version="1.0" ?>
?
<response>
<error>1</error>
<message>invalid url.</message>
</response>

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

public class myapplication extends application{
  private string thing = null;
  public string getthing(){
   return thing;
  }
  public void setthing( string thing ){
   this.thing = thing;
  }
}
public class myactivity extends activity {
   private myapplication app;
   public void oncreate(bundle savedinstancestate) {
     super.oncreate(savedinstancestate);
     app = ((myapplication)getapplication());
     string thing = app.getthing();
   }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android视图view技巧总结》、《android布局layout技巧总结》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。