20160721最近三天安卓开发遇到的问题

来源:互联网 发布:网络摄监控系统图 编辑:程序博客网 时间:2024/06/05 06:40

这三天完成的工作:

1.修改主界面的LinearLayout布局,使之满足基本需求。

解决了一个DeadObject.exception的问题

是因为硬件加速引起的。

设置android:hardwareAccelerated="false"以后,交换LinearLayout以后app就不会崩溃了。


2.自定义了一个button,在button左边加了一个图片。

要修改的地方有两部分

第一个部分是xml,我写了一个线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/imgview"
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:src="@drawable/plus" >
    </ImageView> 


    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:text="点此拍摄更多"
        android:textSize="15sp" >
    </TextView>


</LinearLayout>

第二部分是自定义的java类

package com.example.selfDesignedUI;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import ro.neghina.android.imageFiltering.R;


public class ButtonChoose extends LinearLayout {
    
    private ImageView imgView;  
    private TextView  textView;


    public ButtonChoose(Context context) {
        super(context,null);
    }
    
    public ButtonChoose(Context context,AttributeSet attributeSet) {
        super(context, attributeSet);
        
        LayoutInflater.from(context).inflate(R.layout.button_choose, this,true);
        
        this.imgView = (ImageView)findViewById(R.id.imgview);
        this.textView = (TextView)findViewById(R.id.textview);
        
        this.setClickable(true);
        this.setFocusable(true);
    }
    
    public void setImgResource(int resourceID) {
        this.imgView.setImageResource(resourceID);
    }
    
    public void setText(String text) {
        this.textView.setText(text);
    }
    
    public void setTextColor(int color) {
        this.textView.setTextColor(color);
    }
    
    public void setTextSize(float size) {
        this.textView.setTextSize(size);
    }
    
}

3.修改button的样式,在修改过程中出现了白色背景蓝色边框的边框不完整现象,参考了多个布局文件后,总算是完成了。

主要是填充颜色和线的宽度

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"   
    android:shape="rectangle">  
    <!-- 填充颜色 -->  
    <solid android:color="@color/white"></solid>  
    <!-- 线的宽度,颜色灰色 -->  
    <stroke android:width="1dp" android:color="@color/light_blue"></stroke>          
    <!-- 矩形的圆角半径 -->  
    <corners android:radius="5dp" />         
</shape>  


4. 如何将图片设置为固定的大小:

        android:layout_width="40dp"
        android:layout_height="30dp"
        android:scaleType="fitXY"


5.popupMenu

    如何设置一个popupMenu,先在button上设置弹出一个popupMenu,再设置popupMenu的内容。

btn_tp = (Button) findViewById(R.id.tp_button);
btn_tp.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
showPopupMenu(btn_tp);
}
});

private void showPopupMenu(View view) {


final PopupMenu popupMenu = new PopupMenu(this, view);


popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());


popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_takephoto:
imgUri = capture.dispatchTakePictureIntent(CapturePhoto.SHOT_IMAGE, targetID);
break;
case R.id.action_select:
capture.dispatchTakePictureIntent(CapturePhoto.PICK_IMAGE, targetID);
// imgUri = MainActivity.pickPhoto(targetID, projectName,
// TakephotoActivity.this);
break;
case R.id.action_del:
popupMenu.dismiss();
break;
}
return true;
}
});


6.安卓间共享数据的四种方式

1).基于消息的通信机制  Intent ---bundle ,extra

数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream, 或者LinkList链表等等数据类型就不太好用

bundle和intent.putExtra其实原理是一样的,

intent.putExtra其实是用intent里自带的bundle,如果自己设置了一个新的bundle则用新的bundle代替。


2). 利用static静态数据,public static成员变量;


3).基于外部存储的传输,  File/Preference/ Sqlite ,如果要针对第三方应用需要Content Provider


4).基于IPC的通信机制context 与Service之间的传输,如Activity与Service之间的通信,定义AIDL接口文件。


5). 基于Application Context

首先在AndroidManifest中添加

<application        android:name=".ApplicationTrans"//必须设置>
</application>
然后声明一个类继承Application,这样在全局都可以获取这个类中的数据。

import android.app.Application;public class ApplicationTrans extends Application {    private String value;    public String getValue() {        return value;    }    public void setValue(String value) {        this.value = value;    }}

((ApplicationTrans) getApplication()).getValue();

7.bitmap在Activity之间的传递

发送端:

//先设置ImageView中的setDrawingCacehEnabled(true),这样就可以获取ImageView中的图片

btn_License0.getImageView().setDrawingCacheEnabled(true);

//再将ImageView中的图片保存成一个bitmap
bmp = Bitmap.createBitmap(btn_License0.getImageView().getDrawingCache());

//清除ImageView缓存中的图片,防止下次获取时还是相同的图片
btn_License0.getImageView().setDrawingCacheEnabled(false);

//用一个字节数组保存bitmap
baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
bitmapByte = baos.toByteArray();

//将字节数组加入intent中

intent.putExtra("bitmap", bitmapByte);

接收端:

//将字节数组转换成图片

byte[] bis = intent.getByteArrayExtra("bitmap");
Bitmap bitmap = BitmapFactory.decodeByteArray(bis, 0, bis.length);
img.setImageBitmap(bitmap);

0 0
原创粉丝点击