Back to project page android-component-location.
The source code is released under:
MIT License
If you think the Android project android-component-location listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yingchn.android.activity; //from ww w . ja v a 2 s . co m import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.ActionBarActivity; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.Button; import android.widget.TextView; import com.nineoldandroids.view.animation.AnimatorProxy; import com.sothree.slidinguppanel.SlidingUpPanelLayout; import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelSlideListener; import com.yingchn.android.AppContext; import com.yingchn.android.fragment.SettingFragment; import com.yingchn.android.location.MyLocationManager; import com.yingchn.android.location.R; import com.yingchn.android.util.FragmentUtil; public class MainActivity extends ActionBarActivity { private SlidingUpPanelLayout mLayout; private TextView mPrintTV; private StringBuffer mPrintSB = new StringBuffer(); private BroadcastReceiver mPrintReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(MyLocationManager.BIF_GOT_LOCATION)) { String log = intent.getStringExtra(MyLocationManager.INTENT_PARAMS_LOC_LOG); mPrintSB.append(log); refreshLog(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } mPrintTV = (TextView) findViewById(R.id.tv_log); mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout); mLayout.setPanelSlideListener(new PanelSlideListener() { @Override public void onPanelSlide(View panel, float slideOffset) { setActionBarTranslation(mLayout.getCurrentParalaxOffset()); } @Override public void onPanelExpanded(View panel) { } @Override public void onPanelCollapsed(View panel) { } @Override public void onPanelAnchored(View panel) { } }); MyLocationManager.getInstance().init(); IntentFilter filter = new IntentFilter(); filter.addAction(MyLocationManager.BIF_GOT_LOCATION); LocalBroadcastManager.getInstance(AppContext.getContext()).registerReceiver(mPrintReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); MyLocationManager.getInstance().destory(); } public void refreshLog() { mPrintTV.setText(mPrintSB.toString()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { FragmentUtil.navigateToInNewActivity(this, new SettingFragment(), null); return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { private Button mBtnBaidu, mBtnBaiduCfg; private Button mBtnAmap, mBtnAmapCfg; public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); mBtnBaidu = (Button) rootView.findViewById(R.id.btn_baidu); mBtnAmap = (Button) rootView.findViewById(R.id.btn_amap); mBtnBaiduCfg = (Button) rootView.findViewById(R.id.btn_baidu_cfg); mBtnAmapCfg = (Button) rootView.findViewById(R.id.btn_amap_cfg); return rootView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { } } private int getActionBarHeight(){ int actionBarHeight = 0; TypedValue tv = new TypedValue(); if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); } return actionBarHeight; } @SuppressLint("NewApi") public void setActionBarTranslation(float y) { // Figure out the actionbar height int actionBarHeight = getActionBarHeight(); // A hack to add the translation to the action bar ViewGroup content = ((ViewGroup) findViewById(android.R.id.content).getParent()); int children = content.getChildCount(); for (int i = 0; i < children; i++) { View child = content.getChildAt(i); if (child.getId() != android.R.id.content) { if (y <= -actionBarHeight) { child.setVisibility(View.GONE); } else { child.setVisibility(View.VISIBLE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { child.setTranslationY(y); } else { AnimatorProxy.wrap(child).setTranslationY(y); } } } } } }