標籤:android
一.介面實現:
借鑒了別人的執行個體,然後記錄下引導介面的實現,總體來說實現不算困難,前提是要有個美工幫你做這些引導圖片(找了張圖片湊合用吧):
主介面:
public class MainActivity extends PromptActivity { //activity的生命週期public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setGuideResId(R.drawable.prompt2);// 添加引導頁}}
引導介面:
public class PromptActivity extends Activity {private int guideResourceId = 0;// 引導頁圖片資源idprivate PromptSharedPreferences psp;@Overrideprotected void onStart() {super.onStart();addGuideImage();// 添加引導頁}//顯示引導圖片public void addGuideImage() {psp = new PromptSharedPreferences();View view = getWindow().getDecorView().findViewById(R.id.my_content_view);// 尋找通過setContentView上的根布局if (view == null)return;if (psp.takeSharedPreferences(this)) {// 有過功能引導,就跳出return;}ViewParent viewParent = view.getParent();if (viewParent instanceof FrameLayout) {final FrameLayout frameLayout = (FrameLayout) viewParent;if (guideResourceId != 0) {// 設定了引導圖片final ImageView guideImage = new ImageView(this);FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);guideImage.setLayoutParams(params);guideImage.setScaleType(ScaleType.FIT_XY);guideImage.setImageResource(guideResourceId);guideImage.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//刪除引導圖片frameLayout.removeView(guideImage);//儲存記錄psp.saveSharedPreferences(PromptActivity.this, "啟動了");}});frameLayout.addView(guideImage);// 添加引導圖片}}}//獲得圖片idprotected void setGuideResId(int resId) {this.guideResourceId = resId;}}
布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@id/my_content_view" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="58dp" android:layout_marginLeft="150dp" android:text="哈哈哈哈" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="58dp" android:layout_marginLeft="275dp" android:text="哈" /></RelativeLayout>
本地檔案:
public class PromptSharedPreferences {private SharedPreferences sp;// 儲存public void saveSharedPreferences(Context context, String save) {sp = context.getSharedPreferences("prompt", context.MODE_PRIVATE);Editor editor = sp.edit();editor.putString("state", save);editor.commit();// 儲存新資料}// 取出public boolean takeSharedPreferences(Context context) {String str = "";sp = context.getSharedPreferences("prompt", context.MODE_PRIVATE);str = sp.getString("state", "");if (str.equals("")) {return false;}else{ return true;}}}
添加values/ids.xml:
<?xml version="1.0" encoding="utf-8"?><resources><item type="id" name="my_content_view"></item></resources>
android 功能引導介面實現