android开发小汇4

在android下启动Service
Start_Service.java 外部引用原始文档
1
2
3
4
5
6
//Starts a service (task to be accomplished in the background, without UI)
//The class employing the snippet code must implement ServiceConnection
Intent iServ = new Intent();
iServ.setClass(getBaseContext(), ServiceName.class); //TODO Replace 'ServiceName' with the class name for your Service
bindService(iServ, this, BIND_AUTO_CREATE);
startService(iServ);	


百度地图点mapView获取经纬度
1
2
3
在onCreate()里调mMapView.getOverlays().add(new GetOverlay());

源码:[http://www.eoeandroid.com/thread-160682-1-2.html](http://www.eoeandroid.com/thread-160682-1-2.html)
GetOverlay.java 外部引用原始文档
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class GetOverlay extends Overlay{
                GeoPoint geo;
                @Override
                public void draw(Canvas canvas, MapView mapView, boolean arg2) {
                        super.draw(canvas, mapView, arg2);
                        if(geo==null){
                                return;
                        }
                        Log.d(toString(), arg2+"-------draw--");
                        Projection projection = mapView.getProjection(); 
                        // 把经纬度变换到相对于MapView左上角的屏幕像素坐标
                        Point point = projection.toPixels(geo, null); 
                        // 可在此处添加您的绘制代码
                        Paint paintText = new Paint();
                        paintText.setColor(Color.RED);
                        paintText.setTextSize(35);
                        canvas.drawText("●", point.x-9, point.y+13, paintText); // 绘制文本
                }

                @Override
                public boolean onTap(GeoPoint geo, MapView arg1) {
                        Log.d(this.toString(), geo.getLongitudeE6()/1E6+"----------"+geo.getLatitudeE6()/1E6);
                        return super.onTap(geo, arg1);
                }
                
        }

地图, View, Canvas, 屏幕, 像素


Android 自定义Toast提示消息的图标
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/* 用Toast方式显示 */
Toast toast = Toast.makeText(EX05_07.this, mTextView.getText(),
        Toast.LENGTH_LONG);
View textView = toast.getView();
lay.setOrientation(LinearLayout.HORIZONTAL);
/* 在Toast里加上图片 */
mView01.setImageResource(R.drawable.icon);
/* 在Toast里显示图片 */lay.addView(mView01);
/* 在Toast里显示文字 */lay.addView(textView);
toast.setView(lay);
toast.show();



android圆角
1
实现android圆角
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  
    <solid android:color="#ffffff" />  
  
    <corners  
        android:bottomLeftRadius="20dp"  
        android:bottomRightRadius="20dp"  
        android:topLeftRadius="20dp"  
        android:topRightRadius="20dp" />  
  
</shape>  



实现IOS圆角风格的列表ListView
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
这段代码目前已经加在我的一个jarandroidkit中,还没发布。
适用于android1.6以上,不依赖其他jar

使用时不需要继承这里的RoundListAdapter。只需要在你实现了ListAdapter的类中,传入一个RoundParams的对象,并在getView方法返回前调用这里RoundListAdapter类提供的静态方法。
RoundListAdapter.setItemBackground(position, switcherView, mParams,
getCount()); 

/*
	 * @(#)RoundListAdapter.java               Project:com.sinaapp.msdxblog.androidkit	 * Date:2012-12-6
	 *
	 * Copyright (c) 2011 CFuture09, Institute of Software,
	 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
	 * All rights reserved.
	 *
	 * Licensed under the Apache License, Version 2.0 (the "License");
	 *  you may not use this file except in compliance with the License.
	 * You may obtain a copy of the License at
	 *
	 *     http://www.apache.org/licenses/LICENSE-2.0
	 *
	 * Unless required by applicable law or agreed to in writing, software
	 * distributed under the License is distributed on an "AS IS" BASIS,
	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 * See the License for the specific language governing permissions and
	 * limitations under the License.
	 */
	package com.lurencun.cfuture09.androidkit.widget.roundlist;
	 
	import android.view.View;
	import android.widget.ListAdapter;
	 
	/**
	 * @author Geek_Soledad (66704238@51uc.com)
	 */
	public abstract class RoundListAdapter implements ListAdapter {
	    /**
	     * 圆角ListView的参数类。定义了顶部背景,底部背景,中间背景及单独一个时的背景。
	     *
	     * @author msdx
	     *
	     */
	    public static class RoundParams {
	        public int topResid;
	        public int middleResid;
	        public int bottomResid;
	        public int lonelyResid;
	 
	        public RoundParams(int topResid, int middleReside, int bottomResid,
	                int lonelyResid) {
	            this.topResid = topResid;
	            this.middleResid = middleReside;
	            this.bottomResid = bottomResid;
	            this.lonelyResid = lonelyResid;
	        }
	    }
	 
	    public static void setItemBackground(int position, View item,
	            final RoundParams mParams, final int count) {
	        if (count == 1) {
	            item.setBackgroundResource(mParams.lonelyResid);
	        } else if (position > 0 && position < count - 1) {
	            item.setBackgroundResource(mParams.middleResid);
	        } else if (position == 0) {
	            item.setBackgroundResource(mParams.topResid);
	        } else {
	            item.setBackgroundResource(mParams.bottomResid);
	        }
	    }
	}



Looper和handler使用
LooperThread.java 外部引用原始文档
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class LooperThread extends Thread {
      public Handler mHandler;
 
      public void run() {
          Looper.prepare();
 
          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };
 
          Looper.loop();
      }
  }


Android图片拖动
MainActivity.java 外部引用原始文档
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.cndemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private float mx;
	private float my;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		super.onCreate(savedInstanceState);

		final ImageView switcherView = (ImageView) this.findViewById(R.id.img);

		switcherView.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View arg0, MotionEvent event) {

				float curX, curY;

				switch (event.getAction()) {

				case MotionEvent.ACTION_DOWN:
					mx = event.getX();
					my = event.getY();
					break;
				case MotionEvent.ACTION_MOVE:
					curX = event.getX();
					curY = event.getY();
					switcherView.scrollBy((int) (mx - curX), (int) (my - curY));
					mx = curX;
					my = curY;
					break;
				case MotionEvent.ACTION_UP:
					curX = event.getX();
					curY = event.getY();
					switcherView.scrollBy((int) (mx - curX), (int) (my - curY));
					break;
				}

				return true;
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/img8" />

</RelativeLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值