好久没写blog了,今天把一个FM的项目框架界面分享下,大家都知道,FM是根据每个芯片来工作的,所以我分享的只是界面布局,以及一个嵌套的UI线程更新练习.下面是界面显示
直接上代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yijun" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name"
>
<activity android:name=".ButtonLayout"
android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这里注意一个地方:
android:theme="@android:style/Theme.NoTitleBar" 它是在Activity里的,所以在这个Activity里是没有Title的,如果写在application里,那整个程序就是没有Title的
下面是布局文件,由于布局文件过长,就截个Outline的图片,这里有点值得注意,如果你想要一个透明图片的叠加,可以使用RelativeLayout布局
这里要注意的是一个地方,frequency_view这个是一个view,写在布局文件里,但实现是在代码中实现的
ButtonLayout.java
FrequencyView.java主要就是画下面这个的:package com.yijun; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; /** * * @author Yijun * */ public class ButtonLayout extends Activity implements Runnable, OnClickListener { private static final String TAG = "ButtonLayout"; public static ImageView rssi; static int flg = 0; public Handler myhHandler; public static int screenWidth = 0; private final static int[] mButtonID = { R.id.new_btn_tune_left, R.id.new_btn_tune_right, R.id.new_btn_list, R.id.new_btn_tune_prev, R.id.new_btn_tune_next, R.id.new_btn_add }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_ui); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; //用于UI更新 Looper looper = Looper.myLooper(); myhHandler=new MessageHandler(); new Thread(this).start(); //所有按钮注册监听 for (int i = 0; i < mButtonID.length; i++) { Button btn = (Button) findViewById(mButtonID[i]); btn.setOnClickListener(this); } } //新线程发送消息给UI线程,进行更新画面 @Override public void run() { while(true) { Message message = new Message(); String string = Thread.currentThread().getName(); Log.i(TAG, "run"+string); try { if (flg == 0) { message.what = 1; } else if (flg == 1) { message.what = 2; } else if (flg == 2){ message.what =3; } else if (flg == 3) { message.what =4; } else { message.what =5; } myhHandler.sendMessage(message); flg++; Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //接受从其他线程发来的消息,来进行UI更新 class MessageHandler extends Handler { @Override public void handleMessage(Message msg) { String string = Thread.currentThread().getName(); Log.i(TAG, "MessageHandler"+string); //对消息进行判断 if (msg.what == 1) { ImageView imageView = (ImageView) findViewById(R.id.rssi); TextView textView = (TextView) findViewById(R.id.scan); textView.setVisibility(1); imageView.setImageDrawable(getResources().getDrawable(R.drawable.xinhao_01)); textView.setText("搜索中......"); } else if (msg.what == 2) { ImageView imageView = (ImageView) findViewById(R.id.rssi); TextView textView = (TextView) findViewById(R.id.scan); textView.setVisibility(1); imageView.setImageDrawable(getResources().getDrawable(R.drawable.xinhao_02)); textView.setText("没有找到....."); } else if (msg.what == 3) { ImageView imageView = (ImageView) findViewById(R.id.rssi); TextView textView = (TextView) findViewById(R.id.scan); textView.setVisibility(1); imageView.setImageDrawable(getResources().getDrawable(R.drawable.xinhao_03)); textView.setText("继续搜索中...."); } else if (msg.what == 4) { ImageView imageView = (ImageView) findViewById(R.id.rssi); TextView textView = (TextView) findViewById(R.id.scan); textView.setVisibility(1); imageView.setImageDrawable(getResources().getDrawable(R.drawable.xinhao_04)); textView.setText("........"); } else if (msg.what == 5) { ImageView imageView = (ImageView) findViewById(R.id.rssi); TextView textView = (TextView) findViewById(R.id.scan); textView.setVisibility(1); imageView.setImageDrawable(getResources().getDrawable(R.drawable.xinhao_04)); textView.setText("all the background"); flg =0; } } } //下面5个按钮 @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.new_btn_list: break; case R.id.new_btn_add: break; case R.id.new_btn_tune_prev: break; case R.id.new_btn_tune_next: break; case R.id.btn_power: break; default: break; } } }
//构造函数一般是有两个参数的那个
package com.yijun; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.Align; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; /** * * @author Yijun * FrequencyView继承View * */ class FrequencyView extends View { private int screenWidth = 320; private int mPosition = 0; private int mPositionMove = 0; private int mFrequency = 107500; public final static double percentage = 0.7; private String mName = ""; private Paint mTextPaint = null; private Drawable mDrawableContaner = null; public FrequencyView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mTextPaint = new Paint(); mTextPaint.setLinearText(false); mTextPaint.setAntiAlias(true); mTextPaint.setSubpixelText(false); mTextPaint.setColor(Color.WHITE); mTextPaint.setTextSize(12); mDrawableContaner = context.getResources() .getDrawable(R.drawable.box_0); } @Override protected void onDraw(Canvas canvas) { screenWidth = ButtonLayout.screenWidth; if (mPosition == 0) { mPosition = screenWidth / 10; } canvas.save(); drawContainer(canvas); drawName(canvas, mTextPaint); // drawRssi(canvas, mTextPaint); drawFrequency(canvas, mTextPaint); drawBall(canvas, mTextPaint); drawBack(canvas, mTextPaint); canvas.restore(); } private void drawRssi(Canvas canvas, Paint mTextPaint2) { // TODO Auto-generated method stub } private void drawBack(Canvas canvas, Paint mTextPaint2) { Bitmap start = BitmapFactory.decodeResource(getResources(), R.drawable.start); Bitmap f1 = BitmapFactory.decodeResource(getResources(), R.drawable.f1); Bitmap f2 = BitmapFactory.decodeResource(getResources(), R.drawable.f2); Bitmap f3 = BitmapFactory.decodeResource(getResources(), R.drawable.f3); Bitmap f4 = BitmapFactory.decodeResource(getResources(), R.drawable.f4); Bitmap end2 = BitmapFactory.decodeResource(getResources(), R.drawable.end2); canvas.drawBitmap(start, 15, 210, mTextPaint2); canvas.drawBitmap(f1, new Float(percentage*screenWidth*(3.5/21)+screenWidth/10), 210, mTextPaint2); canvas.drawBitmap(f2, new Float(percentage*screenWidth*(7.5/21)+screenWidth/10-3), 210, mTextPaint2); canvas.drawBitmap(f3, new Float(percentage*screenWidth*(11.5/21)+screenWidth/10-3), 210, mTextPaint2); canvas.drawBitmap(f4, new Float(percentage*screenWidth*(16.5/21)+screenWidth/10-5), 210, mTextPaint2); canvas.drawBitmap(end2, new Float(percentage*screenWidth+screenWidth/10-3), 210, mTextPaint2); } private void drawBall(Canvas canvas, Paint mTextPaint2) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ball); Bitmap bitmaplight = BitmapFactory.decodeResource(getResources(), R.drawable.ball_light); Bitmap line = BitmapFactory.decodeResource(getResources(), R.drawable.beijing_line); canvas.drawBitmap(bitmaplight, mPosition-4, 196, mTextPaint2); canvas.drawBitmap(bitmap, screenWidth/10, 200, mTextPaint2); canvas.drawBitmap(bitmap, new Float(percentage*screenWidth*(3.5/21)+screenWidth/10), 200, mTextPaint2); canvas.drawBitmap(bitmap, new Float(percentage*screenWidth*(7.5/21)+screenWidth/10), 200, mTextPaint2); canvas.drawBitmap(bitmap, new Float(percentage*screenWidth*(11.5/21)+screenWidth/10), 200, mTextPaint2); canvas.drawBitmap(bitmap, new Float(percentage*screenWidth*(16.5/21)+screenWidth/10), 200, mTextPaint2); canvas.drawBitmap(bitmap, new Float(percentage*screenWidth+screenWidth/10), 200, mTextPaint2); canvas.drawBitmap(line, 0, 200, mTextPaint2); } private void drawFrequency(Canvas canvas, Paint mTextPaint2) { int dev=mFrequency/100; if(dev/1000>=1) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), getMap(1)); canvas.drawBitmap(bitmap, mPositionMove+0, 80, mTextPaint2); Bitmap bitmap0 = BitmapFactory.decodeResource(getResources(), getMap(0)); canvas.drawBitmap(bitmap0, mPositionMove+60, 80, mTextPaint2); } else { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), getMap(dev/100)); canvas.drawBitmap(bitmap, mPositionMove+65, 80, mTextPaint2); } Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), getMap((dev%100)/10)); canvas.drawBitmap(bitmap1, mPositionMove+105, 80, mTextPaint2); Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), getMap(10)); canvas.drawBitmap(bitmap2, mPositionMove+150, 80, mTextPaint2); Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(), getMap((dev%100)%10)); canvas.drawBitmap(bitmap3, mPositionMove+170, 80, mTextPaint2); Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.mhz); canvas.drawBitmap(bitmap4, mPositionMove+210, 120, mTextPaint2); } private void drawName(Canvas canvas, Paint mTextPaint2) { Bitmap bitmap = Bitmap.createBitmap(200, 25, Bitmap.Config.ARGB_8888); bitmap = drawTextAtBitmap(bitmap, mName); canvas.drawBitmap(bitmap, mPositionMove, 15, mTextPaint2); } private void drawContainer(Canvas canvas) { Rect r = new Rect(); r.left = 0; r.top = 0; r.right = mPosition + 285; r.bottom = r.top + 200; mDrawableContaner.setBounds(r); mDrawableContaner.draw(canvas); } private Bitmap drawTextAtBitmap(Bitmap bitmap, String text) { int x = bitmap.getWidth(); int y = bitmap.getHeight(); Bitmap newbit = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(newbit); Paint paint = new Paint(); canvas.drawBitmap(bitmap, 0, 0, paint); paint.setColor(Color.parseColor("#dedbde")); paint.setTextSize(20); paint.setTextAlign(Align.CENTER); canvas.drawText(text, x >> 1, 15, paint); return newbit; } private int getMap(int i){ switch (i){ case 0: return R.drawable.n0; case 1: return R.drawable.n1; case 2: return R.drawable.n2; case 3: return R.drawable.n3; case 4: return R.drawable.n4; case 5: return R.drawable.n5; case 6: return R.drawable.n6; case 7: return R.drawable.n7; case 8: return R.drawable.n8; case 9: return R.drawable.n9; case 10: return R.drawable.dian; default: return 0; } } }
以上都是一些表面的界面,实际操作的根据每个手机芯片不同就不写了..其实一开始写这个联系只是为了试验,UI线程更新..结果写伐写伐就成这样了,UI线程,handler,thread,message神马的要继续练习..今天就到这里了