十二个android编程技巧

本文介绍了Java编程中图片透明处理、发送邮件、屏幕亮度控制、过滤文本、ScrollView滚动事件处理、图片显示、文件操作、文本外观设置、特定程序启动方式、圆角图片创建、通知图标提示未读消息、背景渐变设置等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.让一个图片透明:
Java代码

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

2.直接发送邮件:
Java代码

1. Intent intent = new Intent(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.LayoutParams lp = getWindow().getAttributes();
2. lp.screenBrightness = 100 / 100.0f;
3. getWindow().setAttributes(lp);

4.过滤特定文本
Java代码

1. Filter filter = myAdapter.getFilter();
2. filter.filter(mySearchText);

5.scrollView scroll停止事件
Java代码

1. setOnScrollListener(new OnScrollListener(){
2. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
3. // TODO Auto-generated method stub }
4. public void onScrollStateChanged(AbsListView view, int scrollState) {
5. // TODO Auto-generated method stub
6. if(scrollState == 0) Log.i("a", "scrolling stopped..."); } });}

6. 对于特定的程序 发起一个关联供打开
C/C++代码

1. Bitmap bmp = getImageBitmap(jpg);
2. String path = getFilesDir().getAbsolutePath() + "/test.png";
3. File file = new File(path);
4. FileOutputStream fos = new FileOutputStream(file);
5. bmp.compress( CompressFormat.PNG, 100, fos );
6. fos.close();
7.
8. Intent intent = new Intent();
9. intent.setAction(android .content.Intent.ACTION_VIEW);
10. intent.setDataAndType(Uri .fromFile(new File(path)), "image/png");
11. startActivity(intent);
12. 对于图片上边的不适用索引格式会出错。
13. Intent intent = new Intent();
14. intent.setAction(android .content.Intent.ACTION_VIEW);
15. File file = new File("/sdcard/test.mp4");
16. intent.setDataAndType(Uri .fromFile(file), "video/*");
17. startActivity(intent);
18.
19. Intent intent = new Intent();
20. intent.setAction(android .content.Intent.ACTION_VIEW);
21. File file = new File("/sdcard/test.mp3");
22. intent.setDataAndType(Uri .fromFile(file), "audio/*");
23. 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.
7. Intent i = new Intent();
8. i.putExtra(EXTRA_KEY_ARTIST, id);
9. i.setClass(this, ArtistActivity.class);
10. i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
11. startActivity(i);

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

1. Bitmap myCoolBitmap = ... ;
2. int w = myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();
3. Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
4. Canvas canvas = new Canvas(rounder);
5. Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
6. xferPaint.setColor(Color.RED);
7. canvas.drawRoundRect(new RectF(0,0,w,h), 20.0f, 20.0f, xferPaint);
8. xferPaint.setXfermode(new PorterDuffXfermode(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. Notification notification = new Notification (icon, tickerText, when);
2. notification .number = 4;

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

1. <?xml version="1.0" encoding="utf-8"?>
2. <shape xmlns:android ="http://schemas.android .com/apk/res/android " android :shape="rectangle">
3. <gradient android :startColor="#FFFFFFFF" android :endColor="#FFFF0000"
4. android :angle="270"/>
5. </shape>

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

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

1. <?xml version="1.0" encoding="utf-8"?>
2. <resources>
3. <style name="NewTheme" parent="android :Theme">
4. <item name="android :background">@drawable/shape</item>
5. </style>
6. </resources>

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

1. <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
Java代码

1. public class MyApplication extends Application{
2. private String thing = null;
3. public String getThing(){
4. return thing;
5. }
6. public void setThing( String thing ){
7. this.thing = thing; }
8. }
9. public class MyActivity extends Activity {
10. private MyApplication app;
11. public void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. app = ((MyApplication)getApplication());
14. String thing = app.getThing();
15. }
16. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值