FM界面-项目模块分享
2011-12-26 20:57
155 查看
好久没写blog了,今天把一个FM的项目框架界面分享下,大家都知道,FM是根据每个芯片来工作的,所以我分享的只是界面布局,以及一个嵌套的UI线程更新练习.下面是界面显示
直接上代码:
AndroidManifest.xml
这里注意一个地方:
直接上代码:
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
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; } } }FrequencyView.java主要就是画下面这个的:
//构造函数一般是有两个参数的那个 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神马的要继续练习..今天就到这里了
相关文章推荐
- 循序渐进开发WinForm项目(4)--Winform界面模块的集成使用
- 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(7.2) 模块管理,模块的添加、修改、删除
- ios客户端发现_动画屋后期页面重构与悬浮评论分享模块开发项目总结
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之五--模块(菜单)管理源码分享
- 项目的收尾,分享等,轮播图无限循环,即纯图片的界面,三级缓存等
- 去年一个百万级的小软件项目经验分享,20来个功能模块,项目不太好做有些棘手 推荐
- react项目实战(权限模块开发三) 登陆界面开发
- 开发报表模块,与大家分享一些界面
- 分享在项目中用到的一个用于管理缓存文件的模块
- ReactNative项目分享(2)封装网络模块
- 去年一个百万级的小软件项目经验分享,20来个功能模块,项目不太好做有些棘手
- 去年一个百万级的小软件项目经验分享,20来个功能模块,项目不太好做有些棘手
- 去年一个百万级的小软件项目经验分享,20来个功能模块,项目不太好做有些棘手
- 去年一个百万级的小软件项目经验分享,20来个功能模块,项目不太好做有些棘手
- 淘忆项目之引导界面的分享归纳
- 一个比较酷的项目界面分享,干货十足
- golang项目邮件发送模块代码分享
- 淘忆项目之分享界面服务器端的修正归纳
- 去年一个百万级的小软件项目经验分享,20来个功能模块,项目不太好做有些棘手 zhuan zai
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之五--模块(菜单)管理源码分享