編輯:關於Android編程
package com.finals.teltem.view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.EditText; import com.finals.teltem.R; public class SearchView extends EditText { float searchSize = 0; float textSize = 0; int textColor = 0xFF000000; Drawable mDrawable; Paint paint; public SearchView(Context context, AttributeSet attrs) { super(context, attrs); InitResource(context, attrs); InitPaint(); } private void InitResource(Context context, AttributeSet attrs) { TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.searchedit); float density = context.getResources().getDisplayMetrics().density; searchSize = mTypedArray.getDimension(R.styleable.searchedit_imagewidth, 18 * density + 0.5F); textColor = mTypedArray.getColor(R.styleable.searchedit_textColor, 0xFF848484); textSize = mTypedArray.getDimension(R.styleable.searchedit_textSize, 14 * density + 0.5F); mTypedArray.recycle(); } private void InitPaint() { paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(textColor); paint.setTextSize(textSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); DrawSearchIcon(canvas); } private void DrawSearchIcon(Canvas canvas) { if (this.getText().toString().length() == 0) { float textWidth = paint.measureText("搜索"); float textHeight = getFontLeading(paint); float dx = (getWidth() - searchSize - textWidth - 8) / 2; float dy = (getHeight() - searchSize) / 2; canvas.save(); canvas.translate(getScrollX() + dx, getScrollY() + dy); if (mDrawable != null) { mDrawable.draw(canvas); } canvas.drawText("搜索", getScrollX() + searchSize + 8, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint); canvas.restore(); } } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (mDrawable == null) { try { mDrawable = getContext().getResources().getDrawable(com.finals.teltem.R.drawable.serarch); mDrawable.setBounds(0, 0, (int) searchSize, (int) searchSize); } catch (Exception e) { } } } @Override protected void onDetachedFromWindow() { if (mDrawable != null) { mDrawable.setCallback(null); mDrawable = null; } super.onDetachedFromWindow(); } public float getFontLeading(Paint paint) { FontMetrics fm = paint.getFontMetrics(); return fm.bottom - fm.top; } }
安卓手機忘記鎖屏圖案密碼六種解決辦法,小編就來一一說明。 第一種方法:用別人的手機打你的電話, 然後手機就會進入系統,再進設置裡去掉自動鎖屏。當提示輸入
XML對開發者來說十分的方便,不僅使用起來簡單,而且能夠及時調試,修改界面之後馬上能看到效果。Java設置布局不具有這個優勢。但是java卻可以動態對布局進行操作,這是x
本文章是基於鴻洋的Android 自定義View (一) 的一些擴展,以及對Android自定義View構造函數詳解裡面內容的一些轉載。首先我們定義一個declare-s
Android模擬器安裝APP出現INSTALL_FAILED_NO_MATCHING_ABIS錯誤解決方案當我們想在電腦的Android模擬器中安裝APP的