android开发小汇4

来源:互联网 发布:后现代 知乎 编辑:程序博客网 时间:2024/05/17 07:01
在android下启动Service
Start_Service.java外部引用原始文档
123456
//Starts a service (task to be accomplished in the background, without UI)//The class employing the snippet code must implement ServiceConnectionIntent iServ = new Intent();iServ.setClass(getBaseContext(), ServiceName.class); //TODO Replace 'ServiceName' with the class name for your ServicebindService(iServ, this, BIND_AUTO_CREATE);startService(iServ);


百度地图点mapView获取经纬度
Readme.txt外部引用原始文档
123
在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 910111213141516171819202122232425262728
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提示消息的图标
Toast.java外部引用原始文档
 1 2 3 4 5 6 7 8 91011
/* 用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圆角
readme外部引用原始文档
1
实现android圆角
11.jpg外部引用原始文档
style.xml外部引用原始文档
 1 2 3 4 5 6 7 8 9101112
<?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
.java外部引用原始文档
 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
这段代码目前已经加在我的一个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 9101112131415
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 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
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;@Overrideprotected 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;}});}@Overridepublic 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;}}
main.xml外部引用原始文档
 1 2 3 4 5 6 7 8 910111213
<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>



原创粉丝点击