1. 移動地圖
地圖的操作實際上操作的是MapWidget控件 ,MapWidget控件繼承自View空間。所以移動和旋轉地圖實際上是對View的操作。
1.1 實現代碼
在第5篇的時候我們創建了一個RoadWayMapWidget類,在這個類中我們需要更改兩個地方實現地圖的勻速移動。
1. 更改MapWidget原有的DecelerateInterpolator(減速動畫)爲LinearInterpolator(勻速動畫)
重寫public RoadWayMapWidget(Context context, String rootMapFolder,int initialZoomLevel)方法
定義方法moveTo實現從一點移動到另一點。
public class RoadWayMapWidget extends MapWidget{
/**小車圖片*/
protected static Bitmap thisCarIcon;
protected Scroller mapMoveScroller;
private LinearInterpolator linearInterpolator;
Handler handler;
/**
*
* @param context
* -context
* @param rootMapFolder
* -地圖資源文件名
* @param initialZoomLevel
* -初始化縮放等級
* @date 2016年4月1日上午9:11:39
*/
public RoadWayMapWidget(Context context, String rootMapFolder,int initialZoomLevel) {
super(context, rootMapFolder, initialZoomLevel);
// TODO Auto-generated constructor stub
// thisCarIcon = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.car_arror)).getBitmap();
thisCarIcon = BitmapFactory.decodeResource(getResources(), R.drawable.car).copy(Bitmap.Config.ARGB_8888, true);
linearInterpolator = new LinearInterpolator();
mapMoveScroller = new Scroller(context, linearInterpolator);
handler = new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what == 0x1)
{
invalidate();
}
}
};
new Timer().schedule(new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
// debug("TimerTask run");
handler.sendEmptyMessage(0x1);
}
},0,50);
}
/**
* 移動地圖,從一個點到另一個點,勻速移動方式
* @author 秦曉宇
* @date 2016年4月1日 上午9:38:13
* @param x_start
* -起始點x座標
* @param y_start
* -起始點y座標
* @param x_end
* -x座標偏移量
* @param y_end
* -y軸偏移量
* @param scrollerDuration
* -移動的時間
*/
public void moveTo(int x_start,int y_start,int x_end,int y_end,int scrollerDuration){
// 更新結束後,使用動畫控制偏移過程
scroller.startScroll(x_start, y_start, x_end, y_end,scrollerDuration);
// 重繪控件
invalidate();
}
}
1.2 主程序使用
public class MainActivity extends Activity {
private void debug(String str){
Log.d("MainActivity",str);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //設置無標題
setContentView(R.layout.activity_main);
final RoadWayMapWidget map = new RoadWayMapWidget(this, "map",15);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
map.getConfig().setZoomBtnsVisible(false);
layout.addView(map);
map.moveTo(0,0,500,0,5000);
}
}
2 旋轉地圖
2.1 旋轉代碼
RoadWayMapWidget 類中添加rotationTo方法
/**
* 地圖旋轉某個角度,勻速旋轉的方式
* @author 秦曉宇
* @date 2016年4月7日 上午8:38:08
* @param start_rotation
* - 開始旋轉的角度
* @param end_rotation
* - 選擇的角度
* @param duration
* - 旋轉過程的時間
*/
public void rotationTo(float start_rotation,float end_rotation,long duration)
{
LinearInterpolator ll = new LinearInterpolator();
ObjectAnimator animator = ObjectAnimator.ofFloat(this, "rotation",start_rotation, end_rotation);
animator.setInterpolator(ll);
animator.setDuration(duration);
animator.start();
}
2.2 主程序使用
public class MainActivity extends Activity {
private void debug(String str){
Log.d("MainActivity",str);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //設置無標題
setContentView(R.layout.activity_main);
final RoadWayMapWidget map = new RoadWayMapWidget(this, "map",15);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
map.getConfig().setZoomBtnsVisible(false);
layout.addView(map);
map.rotationTo(0f,30f,3000);
}
}