校园助手APP--主界面


登陆成功后进入主界面,主界面是个九宫格

 

public classHomeActivity extends Activity implements IAssistantActivity{
      
       private static final String TAG ="HomeActivity";
 
       private GridView myGridView = null;
       private TextView tvUserName = null;
       private CircleImageView user_Icon = null;
       private MenuSlidingDrawer sliding = null;
       private SimpleAdapter simpleAdapter ;
      
       private Button btn_exit = null;
       private Button btn_logout = null;
      
       @Override
       protected void onCreate(BundlesavedInstanceState) {
              super.onCreate(savedInstanceState);
              // 显示标题,以下三行代码顺序不可变
              requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
              super.setContentView(R.layout.home);
              getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar_main); // titlebar为自己标题栏的布局
              // 设置标题
              TextView tvMainTitle = (TextView)findViewById(R.id.main_title);
              tvMainTitle.setText("微高校-WIT");
              init();
       }
 
       @Override
       public void init()  {
 
              tvUserName = (TextView)findViewById(R.id.userName);
              user_Icon = (CircleImageView)findViewById(R.id.user_icon);
             
              //读取默认用户
              if (null == MainService.nowUser) {
                    MainService.nowUser =SharedPreferencesUtil.getLoginUser(HomeActivity.this);
              }
             
//            tvUserName.setText(MainService.nowUser.getUserName());
              tvUserName.setText("张三");
             
              //读取用户的头像,还未完成
              if (-1 <MainService.nowUser.getIconIndex()) {
              user_Icon.setImageDrawable(getResources().getDrawable(MainService.nowUser.getIconIndex()));
              }else {
              user_Icon.setImageDrawable(getResources().getDrawable(R.drawable.portrait_1));
              }
             
              //抽屉效果的菜单
              sliding = (MenuSlidingDrawer)findViewById(R.id.slidingdrawer);;
             
             
              btn_logout = (Button)findViewById(R.id.btn_logout);
              btn_exit = (Button)findViewById(R.id.btn_exit);
             
              btn_logout.setOnClickListener(newOnClickListener() {
                    @Override
                    public void onClick(View v){
                           Intent intent = newIntent(HomeActivity.this, LoginActivity.class);
                           startActivity(intent);
                           overridePendingTransition(SwitchActivityAnim.leftIn(),SwitchActivityAnim.leftOut());
                           HomeActivity.this.finish();
                           MainService.nowUser =null ;
                    }
              });
             
              //退出程序
              btn_exit.setOnClickListener(newOnClickListener() {
                    @Override
                    public void onClick(View v){
                           MainService.appExit(HomeActivity.this);
                           HomeActivity.this.finish();
                    }
              });
             
              //主界面的初始化布局
              this.myGridView = (GridView)findViewById(R.id.myGridView);
             
              int screenWidth  =getWindowManager().getDefaultDisplay().getWidth();
              int screenHeight =getWindowManager().getDefaultDisplay().getHeight();
              LinearLayout.LayoutParams lp = newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//            int letf = (int) (screenWidth*0.06);
              int top = (int)(screenHeight*0.06);
//            int right = (int)(screenWidth*0.06);
              int bottom = (int)(screenHeight*0.02);
              lp.width = (int)screenWidth;
//            lp.width = (int)(screenWidth * 0.9);
              lp.height = (int)(screenHeight *0.6);
              lp.setMargins(0, top, 0, bottom);
//            myGridView.setLayoutParams(lp);
             
              //数据的初始化
              ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String, Object>>();
             
              HashMap<String, Object> map1= new HashMap<String, Object>();
              map1.put("myimgView",R.drawable.main_infoquery);
              map1.put("mytxt",ConstantValues.INFOQUERY);
              list.add(map1);
 
              HashMap<String, Object> map2= new HashMap<String, Object>();
              map2.put("myimgView",R.drawable.main_dingcan);
              map2.put("mytxt",ConstantValues.DINGCAN);
              list.add(map2);
 
              HashMap<String, Object> map3= new HashMap<String, Object>();
              map3.put("myimgView",R.drawable.main_schoolnews);
              map3.put("mytxt",ConstantValues.SCHOOLNEWS);
              list.add(map3);
 
              HashMap<String, Object> map4= new HashMap<String, Object>();
              map4.put("myimgView",R.drawable.main_library);
              map4.put("mytxt",ConstantValues.LIBRARY);
              list.add(map4);
 
              HashMap<String, Object> map5= new HashMap<String, Object>();
              map5.put("myimgView",R.drawable.main_schoolmap);
              map5.put("mytxt",ConstantValues.SCHOOLMAP);
              list.add(map5);
 
              HashMap<String, Object> map6= new HashMap<String, Object>();
              map6.put("myimgView",R.drawable.main_tieba);
              map6.put("mytxt",ConstantValues.TIEBA);
              list.add(map6);
 
              HashMap<String, Object> map7= new HashMap<String, Object>();
              map7.put("myimgView",R.drawable.main_schoolnews);
              map7.put("mytxt",ConstantValues.FEEDBACK);
              list.add(map7);
 
              HashMap<String, Object> map8= new HashMap<String, Object>();
              map8.put("myimgView",R.drawable.main_more);
              map8.put("mytxt",ConstantValues.MORE);
              list.add(map8);
             
              HashMap<String, Object> map9= new HashMap<String, Object>();
              map9.put("myimgView",R.drawable.main_aboutus);
              map9.put("mytxt",ConstantValues.ABOUTUS);
              list.add(map9);
 
              simpleAdapter = newSimpleAdapter(this, list,
                           R.layout.home_grid_layout,new String[] { "myimgView", "mytxt" },
                           new int[] {R.id.myimgView, R.id.mytxt });
 
              myGridView.setAdapter(simpleAdapter);
              myGridView.setOnItemClickListener(newItemClickListener());
             
              myGridView.setSelector(newColorDrawable(Color.TRANSPARENT));
             
             
              sliding.setOnDrawerOpenListener(newOnDrawerOpenListener() {
                    @Override
                    public void onDrawerOpened(){ }
              });
              sliding.setOnDrawerCloseListener(newOnDrawerCloseListener() {
                    @Override
                    public void onDrawerClosed(){ }
              });
 
       }
 
       class ItemClickListener implementsOnItemClickListener {
 
              @Override
              public voidonItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
                    @SuppressWarnings("unchecked")
                    HashMap<String,Object> item = (HashMap<String, Object>) arg0
                                  .getItemAtPosition(index);
                    String txt = (String) item.get("mytxt");
                   
                    Intent intent = null;
                    if(ConstantValues.INFOQUERY.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         InfoQueryActivity.class);
                    } else if(ConstantValues.DINGCAN.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         StoreListActivity.class);
                    } else if(ConstantValues.SCHOOLNEWS.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         NewsActivity.class);
                    } else if(ConstantValues.LIBRARY.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         LibraryActivity.class);
                    } else if(ConstantValues.SCHOOLMAP.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         TencentMapActivity.class);
                    } else if(ConstantValues.TIEBA.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         TieBaActivity.class);
                    } else if(ConstantValues.ABOUTUS.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         TieBaActivity.class);
                    } else if(ConstantValues.FEEDBACK.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         SchoolNewsActivity.class);
                    } else if(ConstantValues.MORE.equals(txt)) {
                           intent = newIntent(HomeActivity.this,
                                         ExpressActivity.class);
                    }
                    Log.i(TAG,view.getClass().getName());
                    startActivity(intent);
                    overridePendingTransition(SwitchActivityAnim.upIn(),SwitchActivityAnim.upOut());
              }
             
       }
 
       //框架中刷新界面的
       @Override
       public void refresh(Object... params) {
              //更新主界面
       }
 
       @Override
       public boolean onKeyDown(int keyCode,KeyEvent event) {
             
              switch (keyCode) {
              case KeyEvent.KEYCODE_BACK:{
                   
                    if (sliding.isOpened()) {
                           sliding.animateClose();
                           return true;
                    }else {
                           moveTaskToBack(false);
                           return false;
                    }
              }
              //菜单键,弹出抽屉
              case KeyEvent.KEYCODE_MENU:{
                    sliding.animateToggle();
                    break;
              }
              default:
                    break;
              }
             
              return super.onKeyDown(keyCode,event);
       }
}



主界面主要就是抽屉的使用,以及用户圆圈头像的处理。

主界面的布局文件内容如下:

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/bg_home2"
    >
 
    <RelativeLayout
        android:id="@+id/grideview"
       android:layout_width="match_parent"
       android:layout_height="match_parent"  >
        <!-- 
       
        <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="120dp"
            android:gravity="left"
           android:layout_alignParentTop="true"
           android:orientation="horizontal" >
 
            <ImageView
               android:id="@+id/weather_img"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:src="@drawable/weather" />
 
            <LinearLayout
                android:layout_width="wrap_content"
               android:layout_height="150dp"
               android:layout_gravity="center"
               android:gravity="center"
               android:orientation="vertical" >
 
                <TextView
                   android:id="@+id/weather_text"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:textColor="@color/color_text"
                    android:textSize="20sp"
                    android:text="多 云" />
 
                <TextView
                   android:id="@+id/weather_temper"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:textColor="@color/color_text"
                   android:textSize="20sp"
                   android:text="11℃~23℃" />
            </LinearLayout>
        </LinearLayout>
        -->
        <GridView
            android:id="@+id/myGridView"
           android:layout_width="match_parent"
           android:layout_height="320dp"
           android:layout_centerInParent="true"
            android:numColumns="3"
            android:padding="10dp"
            android:horizontalSpacing="20dp"
           android:verticalSpacing="20dp" />
    </RelativeLayout>
 
   <cn.edu.wit.withelper.util.MenuSlidingDrawer
       android:id="@+id/slidingdrawer"
       android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       android:content="@+id/content"
        android:handle="@+id/handle"
        android:topOffset="0dp"
       android:allowSingleTap="false"
       android:layout_gravity="bottom"
       android:orientation="vertical" >
 
        <LinearLayout
            android:id="@id/handle"
           android:orientation="horizontal"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" >
 
           <cn.edu.wit.withelper.util.CircleImageView
                android:id="@+id/user_icon"
               android:layout_width="90sp"
               android:layout_height="90sp"
               android:layout_marginLeft="30dp"
               android:src="@drawable/portrait_1" />
 
            <TextView
                android:id="@+id/userName"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:layout_gravity="center_horizontal|center_vertical"
               android:gravity="center"
               android:textColor="@color/white"
               android:textSize="25sp"
                android:text="极星文化传媒" />
        </LinearLayout>
 
        <LinearLayout
            android:id="@id/content"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="@color/white"
           android:orientation="vertical" >
 
            <Button
               android:id="@+id/btn_logout"
                android:layout_width="match_parent"
               android:layout_height="wrap_content"
                android:text="注销当前账号"
               android:textSize="25sp"
                android:padding="5dp"
                android:layout_margin="3dp"
               android:textColor="@color/black"
               android:background="@drawable/btn_logout"/>
           
            <Button
               android:id="@+id/btn_exit"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
                android:text="退出"
               android:textSize="25sp"
                android:padding="5dp"
               android:layout_margin="3dp"
               android:textColor="@color/black"
               android:background="@drawable/btn_exit"/>
           
           
        </LinearLayout>
   </cn.edu.wit.withelper.util.MenuSlidingDrawer>
 
</FrameLayout>



中间注释的部分,是准备添加天气到九宫格的上方。

 

抽屉部分是自定义的一个控件

public classMenuSlidingDrawer extends SlidingDrawer {
 
       private boolean mVertical;
       private int mTopOffset;
      
       public static int slidingHight ;
 
       public MenuSlidingDrawer(Context context,AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
              int orientation = attrs.getAttributeIntValue("android","orientation",
                           ORIENTATION_VERTICAL);
              mTopOffset =attrs.getAttributeIntValue("android", "topOffset", 0);//设置抽屉上方间距为0
              mVertical = (orientation ==SlidingDrawer.ORIENTATION_VERTICAL);//判断屏幕方向,方便下面根据屏幕方向计算抽屉的高度
             
       }
 
       public MenuSlidingDrawer(Context context,AttributeSet attrs) {//与上面的构造函数差不多,以前用的时候没有仔细看,这个类可以再精简一些
              super(context, attrs);
              int orientation =attrs.getAttributeIntValue("android", "orientation",
                           ORIENTATION_VERTICAL);
              mTopOffset =attrs.getAttributeIntValue("android", "topOffset", 0);
              mVertical = (orientation ==SlidingDrawer.ORIENTATION_VERTICAL);
       }
 
       @Override
       protected void onMeasure(intwidthMeasureSpec, int heightMeasureSpec) {
              int widthSpecMode =MeasureSpec.getMode(widthMeasureSpec);
              int widthSpecSize =MeasureSpec.getSize(widthMeasureSpec);//获取屏幕的宽度
              int heightSpecMode =MeasureSpec.getMode(heightMeasureSpec);
              int heightSpecSize =MeasureSpec.getSize(heightMeasureSpec);//获取屏幕的高度
 
              final View handle = getHandle();
              final View content = getContent();
              measureChild(handle,widthMeasureSpec, heightMeasureSpec);
 
              if(mVertical) {//竖屏
                   
                    int height = heightSpecSize- handle.getMeasuredHeight() - mTopOffset ;//这时handle的顶端与屏幕顶端的距离
                    content.measure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(height, heightSpecMode));
                    heightSpecSize =handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();//这是整个抽屉控件的高度
                    widthSpecSize =content.getMeasuredWidth();
                    if(handle.getMeasuredWidth() > widthSpecSize)
                           widthSpecSize =handle.getMeasuredWidth();//宽度以handle为准
              } else {//横屏
                    int width = widthSpecSize -handle.getMeasuredWidth() - mTopOffset;
                    getContent().measure(MeasureSpec.makeMeasureSpec(width,widthSpecMode),heightMeasureSpec);
                    widthSpecSize =handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();
                    heightSpecSize =content.getMeasuredHeight();
                    if(handle.getMeasuredHeight() > heightSpecSize)
                           heightSpecSize =handle.getMeasuredHeight();
              }
 
              slidingHight =handle.getMeasuredWidth() + content.getMeasuredWidth();//感觉这句没啥用
              setMeasuredDimension(widthSpecSize,heightSpecSize);
       }
}
 


把头像处理成圆形的也是网上找的一段代码


public classCircleImageView extends ImageView implements View.OnClickListener{
  
//     private static final String TAG ="CircleImageView";
    private Paint mPaint = new Paint();
 
    private static final int STROKE_WIDTH = 8;//圆头像外面的一圈白色环的宽度
 
    public CircleImageView(Context context,AttributeSet attrs) {
        super(context, attrs);
    }
 
 
    @SuppressLint("DrawAllocation")
       @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (null != drawable) {
            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
            Bitmap b =circleBitmap(scaleBitmap(bitmap));//先更改图像的尺寸,避免裁剪圆形时把有用的部分去掉了;再在方形的图片内裁剪一个内切圆,然后在圆形外面加一圈白色的环
            final Rect rect = new Rect(0, 0,b.getWidth(), b.getHeight());
            mPaint.reset();
            canvas.drawBitmap(b, rect, rect,mPaint);
        } else {
            super.onDraw(canvas);
        }
    }
 
    // 将矩形图片绘制成圆形图片并在最外围画一圈白色
    private Bitmap circleBitmap(Bitmap bitmap){
        Bitmap output =Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(),Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
 
        final int color = 0xff000000;
        final Rect rect = new Rect(0, 0,bitmap.getWidth(), bitmap.getHeight());
        mPaint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        mPaint.setColor(color);
        final int width = bitmap.getWidth();
        canvas.drawCircle(width / 2, width / 2,width / 2, mPaint);//内切圆,如果是长方形,则以宽度的一半为半径
        mPaint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect,mPaint);//将图片绘制成白色图片
        //画白色圆圈
        mPaint.reset();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(STROKE_WIDTH);
        mPaint.setAntiAlias(true);
        canvas.drawCircle(width/2, width/2, width/2 - STROKE_WIDTH/2, mPaint);
 
        return output;
    }
 
 
    /**
     * change the bitmap size
     * @param src : Bitmap source
     * @return : The scaled bitmap
     */
    private Bitmap scaleBitmap(Bitmap src) {
        int width = src.getWidth();//原来尺寸大小
        int height = src.getHeight();
        final float destSize =this.getWidth();//缩放到这个大小
 
//图片缩放比例,新尺寸/原来的尺寸
        float scaleWidth = ((float) destSize) /width;
        float scaleHeight = ((float) destSize)/ height;
 
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth,scaleHeight);
 
//返回缩放后的图片
        return Bitmap.createBitmap(src, 0, 0,width, height, matrix, true);
    }
 
    @Override
    public void onClick(View v) {
        //To change body of implemented methodsuse File | Settings | File Templates.
    }
}



有一点特地拿出来说一下,

GridView.setSelector(newColorDrawable(Color.TRANSPARENT));

若没有这一句话,点击选项时会出现橙色的背景,很丑,

解决办法就是手动的把背景设置为透明,或者自定义的图片,来保证主题的统一。同样的还有输入框获得焦点是的橙色边框。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值