`
kingsword
  • 浏览: 7011 次
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
toRoundBitmap
public static Bitmap toRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float roundPx;
        float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
        if (width <= height) {
            roundPx = width / 2;
            top = 0;
            bottom = width;
            left = 0;
            right = width;
            height = width;
            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else {
            roundPx = height / 2;
            float clip = (width - height) / 2;
            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;
            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }

        Bitmap output = Bitmap.createBitmap(width,
                                                   height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
        final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
        final RectF rectF = new RectF(dst);

        paint.setAntiAlias(true);

        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return output;
    }
FileOperations
public synchronized static void writeFile(String lineStr, Context context) {
        File f = context.getFileStreamPath(CACHE_FILE_NAME);
        boolean isFileExist = f.exists();
        try {
            FileOutputStream fout = context.openFileOutput(CACHE_FILE_NAME, Context.MODE_PRIVATE|Context.MODE_APPEND);
            OutputStreamWriter writer = new OutputStreamWriter(fout, "UTF-8");
            if (isFileExist) {
                writer.write(lineStr + "\t\n");
            } else {
                String headerLine = String.format("%s,%s,%s,%s", KEY_1, KEY_2, KEY_3,KEY_4);

                writer.write(headerLine + "\t\n");
                writer.write(lineStr + "\t\n");
            }
            writer.close();
            fout.close();
        } catch (IOException e) {
            Log.e(TAG, "Exception happens", e);
        }
    }

    //读数据  
    public synchronized static String readFile(Context context) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(context.openFileInput(CACHE_FILE_NAME), "UTF-8"));
            String line = "";
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            return sb.toString();
        } catch (IOException e) {
            Log.e(TAG, "Exception happens", e);
        }
        return "empty";
    }

    public synchronized static boolean copyAndDeleteOriginalFile(Context context) {
        try {

            File cacheFile = context.getFileStreamPath(CACHE_FILE_NAME);
            if(!cacheFile.exists()) {
                return false;
            }

            int byteread = 0;
            InputStream inStream = context.openFileInput(CACHE_FILE_NAME);
            FileOutputStream fs
                = context.openFileOutput(BAK_CACHE_FILE_NAME, Context.MODE_PRIVATE);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
            fs.flush();
            fs.close();
            inStream.close();

            //Delete the original file.
            context.deleteFile(CACHE_FILE_NAME);

            return true;
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Exception happens", e);
        } catch (IOException e) {
            Log.e(TAG, "Exception happens", e);
        }
        return false;
    }
Combined Image
<!--
    combined_image.xml, put in drawable folder.
    image_2 将在image_1的右下角
    
-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/icon_1">
        <bitmap
             android:gravity ="center_vertical"
             android:src="@drawable/image_1"/>
    </item>

    <item
        android:id="@+id/icon_2"
        android:left="15.0dip"
        android:top="25.0dip">
        <bitmap
            android:gravity="bottom"
            android:src="@drawable/image_2" />
    </item>

</layer-list>
Get Storage Available Space
public long getInternalStorageAvailableSpace(String storagePath) {
    StatFs stat = new StatFs(storagePath);
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return blockSize * availableBlocks;
}
Basic View Extension
//Java
public class CustomeView extends RelativeLayout {

    public CustomeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initUI(context);
    }

    public CustomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUI(context);
    }

    public CustomeView(Context context) {
        super(context);
        initUI(context);
    }

    private void initUI(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.custome_view, this);
        //Do more
    }
}

//custome_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/v1" >
    </ImageView>

    <ImageView
        android:id="@+id/imageV2"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_alignTop = "@+id/imageV1"
        android:layout_alignLeft = "@+id/imageV1"
        android:layout_marginLeft = "-1dip"
        android:layout_marginTop = "-1dip"
        android:src="@drawable/v2" >
    </ImageView>

</RelativeLayout>

/**
 * RelativeLayout用到的一些重要的属性:
 * 第一类:属性值为true或false
 * android:layout_centerHrizontal 水平居中
 * android:layout_centerVertical 垂直居中
 * android:layout_centerInparent 相对于父元素完全居中
 * android:layout_alignParentBottom 贴紧父元素的下边缘
 * android:layout_alignParentLeft 贴紧父元素的左边缘
 * android:layout_alignParentRight 贴紧父元素的右边缘
 * android:layout_alignParentTop 贴紧父元素的上边缘
 * android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
 * 第二类:属性值必须为id的引用名“@id/id-name”
 * android:layout_below 在某元素的下方
 * android:layout_above 在某元素的的上方
 * android:layout_toLeftOf 在某元素的左边
 * android:layout_toRightOf 在某元素的右边
 * android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
 * android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
 * android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
 * android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
 * 第三类:属性值为具体的像素值,如30dip,40px
 * android:layout_marginBottom 离某元素底边缘的距离
 * android:layout_marginLeft 离某元素左边缘的距离
 * android:layout_marginRight 离某元素右边缘的距离
 * android:layout_marginTop 离某元素上边缘的距离
 */

//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <com.view.research.CustomeView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </com.view.research.CustomeView>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>
Create a dialog
private void createDialog(final Context context, final Runnable callback){
        final View view = LayoutInflater.from(context).inflate(R.layout.xxxx, null);
        final CheckBox box = (CheckBox)view.findViewById(R.id.check_box);
        box.setChecked(true);
        final AlertDialog dialog = new AlertDialog.Builder(context)
       		.setIcon(null)
       		.setTitle(R.string.aTitle)
                .setView(view)
                .setOnCancelListener(new DialogInterface.OnCancelListener(){
                    @Override
		    public void onCancel(DialogInterface dialog) {
                        dialog.dismiss();
                        OutsideActivity.this.finish();
		    }
                }).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Do yes
                        dialog.dismiss();
                    }
                }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        OutsideActivity.this.finish();
                    }
                }).create();
         dialog.show();
    }

//xxxx.xml example content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="@color/white">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:maxLines="10000"
            android:singleLine="false"
            android:text="blablablablabla"/>

    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/check_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:gravity="center"
            android:text="lalalala"/>
    </LinearLayout>

</LinearLayout>
Global site tag (gtag.js) - Google Analytics