private void initDeleteIcons() { int deleteIconViews[] = { R.id.deleteIcon1, R.id.deleteIcon2, R.id.deleteIcon3, R.id.deleteIcon4, R.id.deleteIcon5, R.id.deleteIcon6, R.id.deleteIcon7, R.id.deleteIcon8, R.id.deleteIcon9, R.id.deleteIcon10 }; RelativeLayout.LayoutParams deleteIconParams = new RelativeLayout.LayoutParams(screenHeight / 20, screenHeight / 20); deleteIconParams.alignWithParent = true; deleteIconParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); deleteIconParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); deleteIconParams.bottomMargin = 3; deleteIconParams.rightMargin = 3; for (int i = 0; i < deleteIconViews.length; i++) { ImageView deleteIcon = (ImageView) findViewById(deleteIconViews[i]); deleteIcon.setLayoutParams(deleteIconParams); deleteIcon.setOnClickListener(deleteIconClickListener); deleteIcon.setVisibility(View.GONE); deleteIcons.add(deleteIcon); } }
/** * 初始化 * * @param imgResId 子按鈕嘅圖片drawalbe嘅id[] * @param showhideButtonId 主按鈕嘅圖片drawable嘅id * @param crossId 主按鈕上面嗰個轉動十字嘅圖片drawable嘅id * @param pCode 位置代碼,例如“右上角”係ALIGN_PARENT_BOTTOM|ALIGN_PARENT_RIGHT * @param radius 半徑 * @param durationMillis 動畫耗時 */ public void init( int[] imgResId, int showhideButtonId, int crossId, byte pCode, int radius, final int durationMillis) { // 處理pcode,將自定義嘅位置值改成align值 int align1 = 12, align2 = 14; if (pCode == RIGHTBOTTOM) { // 右下角 align1 = ALIGN_PARENT_RIGHT; align2 = ALIGN_PARENT_BOTTOM; } else if (pCode == CENTERBOTTOM) { // 中下 align1 = CENTER_HORIZONTAL; align2 = ALIGN_PARENT_BOTTOM; } else if (pCode == LEFTBOTTOM) { // 左下角 align1 = ALIGN_PARENT_LEFT; align2 = ALIGN_PARENT_BOTTOM; } else if (pCode == LEFTCENTER) { // 左中 align1 = ALIGN_PARENT_LEFT; align2 = CENTER_VERTICAL; } else if (pCode == LEFTTOP) { // 左上角 align1 = ALIGN_PARENT_LEFT; align2 = ALIGN_PARENT_TOP; } else if (pCode == CENTERTOP) { // 中上 align1 = CENTER_HORIZONTAL; align2 = ALIGN_PARENT_TOP; } else if (pCode == RIGHTTOP) { // 右上角 align1 = ALIGN_PARENT_RIGHT; align2 = ALIGN_PARENT_TOP; } else if (pCode == RIGHTCENTER) { // 右中 align1 = ALIGN_PARENT_RIGHT; align2 = CENTER_VERTICAL; } // 如果細過半徑就整大佢 RelativeLayout.LayoutParams thislps = (LayoutParams) this.getLayoutParams(); Bitmap mBottom = BitmapFactory.decodeResource(mycontext.getResources(), imgResId[0]); if (pCode == CENTERBOTTOM || pCode == CENTERTOP) { if (thislps.width != -1 && thislps.width != -2 && thislps.width < (radius + mBottom.getWidth() + radius * 0.1) * 2) { thislps.width = (int) ((radius * 1.1 + mBottom.getWidth()) * 2); } } else { if (thislps.width != -1 && thislps.width != -2 && thislps.width < radius + mBottom.getWidth() + radius * 0.1) { // -1係FILL_PARENT,-2係WRAP_CONTENT // 因為animation嘅setInterpolator設咗OvershootInterpolator,即系喐到目標之後仍然行多一段(超過目標位置)~然後再縮返到目標位置,所以父layout就要再放大少少。而因為呢個OvershootInterpolator接納嘅係一個彈力(浮點)值,佢經過一定算法計算出個時間……如果要根據呢個彈力轉換做距離數值,就比較麻煩,所以我只系求其加咗1/10個半徑。想追求完美嘅~可以自行研究下OvershootInterpolator類同Animation類,http://www.oschina.net可以揾倒android sdk嘅源碼。 thislps.width = (int) (radius * 1.1 + mBottom.getWidth()); } } if (pCode == LEFTCENTER || pCode == RIGHTCENTER) { if (thislps.height != -1 && thislps.height != -2 && thislps.height < (radius + mBottom.getHeight() + radius * 0.1) * 2) { thislps.width = (int) ((radius * 1.1 + mBottom.getHeight()) * 2); } } else { if (thislps.height != -1 && thislps.height != -2 && thislps.height < radius + mBottom.getHeight() + radius * 0.1) { thislps.height = (int) (radius * 1.1 + mBottom.getHeight()); } } this.setLayoutParams(thislps); // 兩個主要層 RelativeLayout rl1 = new RelativeLayout(mycontext); // 包含若干子按鈕嘅層 rlButton = new RelativeLayout(mycontext); // 主按扭 llayouts = new LinearLayout[imgResId.length]; // N個子按鈕 for (int i = 0; i < imgResId.length; i++) { ImageView img = new ImageView(mycontext); // 子按扭圖片 img.setImageResource(imgResId[i]); LinearLayout.LayoutParams llps = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); img.setLayoutParams(llps); llayouts[i] = new LinearLayout(mycontext); // 子按鈕層 llayouts[i].setId(100 + i); // 隨便設個id,方便onclick嘅時候識別返出嚟。呢個id值係求其設嘅,如果發現同其他控件沖突就自行改一下。 llayouts[i].addView(img); llayouts[i].setVisibility(View.GONE); RelativeLayout.LayoutParams rlps = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); rlps.alignWithParent = true; rlps.addRule(align1, RelativeLayout.TRUE); rlps.addRule(align2, RelativeLayout.TRUE); llayouts[i].setLayoutParams(rlps); rl1.addView(llayouts[i]); } RelativeLayout.LayoutParams rlps1 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); rlps1.alignWithParent = true; rlps1.addRule(align1, RelativeLayout.TRUE); rlps1.addRule(align2, RelativeLayout.TRUE); rl1.setLayoutParams(rlps1); RelativeLayout.LayoutParams buttonlps = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); buttonlps.alignWithParent = true; buttonlps.addRule(align1, RelativeLayout.TRUE); buttonlps.addRule(align2, RelativeLayout.TRUE); rlButton.setLayoutParams(buttonlps); rlButton.setBackgroundResource(showhideButtonId); cross = new ImageView(mycontext); cross.setImageResource(crossId); RelativeLayout.LayoutParams crosslps = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); crosslps.alignWithParent = true; crosslps.addRule(CENTER_IN_PARENT, RelativeLayout.TRUE); cross.setLayoutParams(crosslps); rlButton.addView(cross); myani = new myAnimations(rl1, pCode, radius); rlButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (areButtonsShowing) { myani.startAnimationsOut(durationMillis); cross.startAnimation(myAnimations.getRotateAnimation(-270, 0, 300)); } else { myani.startAnimationsIn(durationMillis); cross.startAnimation(myAnimations.getRotateAnimation(0, -270, 300)); } areButtonsShowing = !areButtonsShowing; } }); cross.startAnimation(myAnimations.getRotateAnimation(0, 360, 200)); this.addView(rl1); this.addView(rlButton); hasInit = true; }
private void init() { mCurrentDirectory = Environment.getExternalStorageDirectory(); mInitialDirectory = Environment.getExternalStorageDirectory(); this.setBackgroundResource(R.drawable.bg_pathbar); this.setInAnimation(getContext(), R.anim.fade_in); this.setOutAnimation(getContext(), R.anim.fade_out); // RelativeLayout1 RelativeLayout standardModeLayout = new RelativeLayout(getContext()); { // I use a block here so that layoutParams can be used as a variable name further down. android.widget.ViewFlipper.LayoutParams layoutParams = new android.widget.ViewFlipper.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); standardModeLayout.setLayoutParams(layoutParams); this.addView(standardModeLayout); } // ImageButton -- GONE. Kept this code in case we need to use an right-aligned button in the // future. mSwitchToManualModeButton = new ImageButton(getContext()); { android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); mSwitchToManualModeButton.setLayoutParams(layoutParams); mSwitchToManualModeButton.setId(10); mSwitchToManualModeButton.setImageResource(R.drawable.ic_navbar_edit); mSwitchToManualModeButton.setBackgroundResource(R.drawable.bg_navbar_btn); mSwitchToManualModeButton.setVisibility(View.GONE); standardModeLayout.addView(mSwitchToManualModeButton); } // ImageButton -- GONE. Kept this code in case we need to use an left-aligned button in the // future. ImageButton cdToRootButton = new ImageButton(getContext()); { android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); cdToRootButton.setLayoutParams(layoutParams); cdToRootButton.setId(11); cdToRootButton.setBackgroundResource(R.drawable.bg_navbar_btn); cdToRootButton.setImageResource(R.drawable.ic_navbar_home); cdToRootButton.setScaleType(ScaleType.CENTER_INSIDE); cdToRootButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { cd("/"); } }); cdToRootButton.setVisibility(View.GONE); standardModeLayout.addView(cdToRootButton); } // Horizontal ScrollView container mPathButtonsContainer = new HorizontalScrollView(getContext()); { android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); layoutParams.addRule(RelativeLayout.LEFT_OF, mSwitchToManualModeButton.getId()); layoutParams.addRule(RelativeLayout.RIGHT_OF, cdToRootButton.getId()); layoutParams.alignWithParent = true; mPathButtonsContainer.setLayoutParams(layoutParams); mPathButtonsContainer.setHorizontalScrollBarEnabled(false); mPathButtonsContainer.setHorizontalFadingEdgeEnabled(true); standardModeLayout.addView(mPathButtonsContainer); } // PathButtonLayout mPathButtons = new PathButtonLayout(getContext()); { android.widget.LinearLayout.LayoutParams layoutParams = new android.widget.LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mPathButtons.setLayoutParams(layoutParams); mPathButtons.setNavigationBar(this); mPathButtonsContainer.addView(mPathButtons); } // RelativeLayout2 RelativeLayout manualModeLayout = new RelativeLayout(getContext()); { android.widget.ViewFlipper.LayoutParams layoutParams = new android.widget.ViewFlipper.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); manualModeLayout.setLayoutParams(layoutParams); this.addView(manualModeLayout); } // ImageButton mGoButton = new ImageButton(getContext()); { android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); mGoButton.setLayoutParams(layoutParams); mGoButton.setId(20); mGoButton.setBackgroundResource(R.drawable.bg_navbar_btn); mGoButton.setImageResource(R.drawable.ic_navbar_accept); mGoButton.setScaleType(ScaleType.CENTER_INSIDE); mGoButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { manualInputCd(mPathEditText.getText().toString()); } }); manualModeLayout.addView(mGoButton); } // EditText mPathEditText = new EditText(getContext()); { android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); layoutParams.alignWithParent = true; layoutParams.addRule(RelativeLayout.LEFT_OF, mGoButton.getId()); mPathEditText.setLayoutParams(layoutParams); mPathEditText.setBackgroundResource(R.drawable.bg_navbar_textfield); mPathEditText.setTextColor(Color.BLACK); mPathEditText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); mPathEditText.setImeOptions(EditorInfo.IME_ACTION_GO); mPathEditText.setOnEditorActionListener( new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_GO || (event.getAction() == KeyEvent.ACTION_DOWN && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER))) { if (manualInputCd(v.getText().toString())) // Since we have successfully navigated. return true; } return false; } }); manualModeLayout.addView(mPathEditText); } }