Android仿微信圖片上傳,可以選擇多張圖片,縮放預覽,拍照上傳等


 

仿照微信,朋友圈分享圖片功能 。可以進行圖片的多張選擇,拍照添加圖片,以及進行圖片的預覽,預覽時可以進行縮放,並且可以刪除選中狀態的圖片 。很不錯的源碼,大家有需要可以下載看看 。

微信

微信

微信

微信

下載地址 : 微信上傳圖片源碼

很多網友不知道怎么獲取圖片路徑,這里貼出來:

String path = Bimp.tempSelectBitmap.get(position).getImagePath();

//部分代碼如下

復制代碼
package com.king.photo.activity;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
 
import com.king.photo.R;
import com.king.photo.util.Bimp;
import com.king.photo.util.FileUtils;
import com.king.photo.util.ImageItem;
import com.king.photo.util.PublicWay;
import com.king.photo.util.Res;
 
 
 
 //首頁面activity
 
public class MainActivity extends Activity {
 
    private GridView noScrollgridview;
    private GridAdapter adapter;
    private View parentView;
    private PopupWindow pop = null;
    private LinearLayout ll_popup;
    public static Bitmap bimap ;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Res.init(this);
        bimap = BitmapFactory.decodeResource(
                getResources(),
                R.drawable.icon_addpic_unfocused);
        PublicWay.activityList.add(this);
        parentView = getLayoutInflater().inflate(R.layout.activity_selectimg, null);
        setContentView(parentView);
        Init();
    }
 
    public void Init() {
        
        pop = new PopupWindow(MainActivity.this);
        
        View view = getLayoutInflater().inflate(R.layout.item_popupwindows, null);
 
        ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
        
        pop.setWidth(LayoutParams.MATCH_PARENT);
        pop.setHeight(LayoutParams.WRAP_CONTENT);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setContentView(view);
        
        RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent);
        Button bt1 = (Button) view
                .findViewById(R.id.item_popupwindows_camera);
        Button bt2 = (Button) view
                .findViewById(R.id.item_popupwindows_Photo);
        Button bt3 = (Button) view
                .findViewById(R.id.item_popupwindows_cancel);
        parent.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                pop.dismiss();
                ll_popup.clearAnimation();
            }
        });
        bt1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                photo();
                pop.dismiss();
                ll_popup.clearAnimation();
            }
        });
        bt2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,
                        AlbumActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);
                pop.dismiss();
                ll_popup.clearAnimation();
            }
        });
        bt3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                pop.dismiss();
                ll_popup.clearAnimation();
            }
        });
        
        noScrollgridview = (GridView) findViewById(R.id.noScrollgridview);    
        noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
        adapter = new GridAdapter(this);
        adapter.update();
        noScrollgridview.setAdapter(adapter);
        noScrollgridview.setOnItemClickListener(new OnItemClickListener() {
 
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                if (arg2 == Bimp.tempSelectBitmap.size()) {
                    Log.i("ddddddd", "----------");
                    ll_popup.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.activity_translate_in));
                    pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
                } else {
                    Intent intent = new Intent(MainActivity.this,
                            GalleryActivity.class);
                    intent.putExtra("position", "1");
                    intent.putExtra("ID", arg2);
                    startActivity(intent);
                }
            }
        });
 
    }
 
    @SuppressLint("HandlerLeak")
    public class GridAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private int selectedPosition = -1;
        private boolean shape;
 
        public boolean isShape() {
            return shape;
        }
 
        public void setShape(boolean shape) {
            this.shape = shape;
        }
 
        public GridAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }
 
        public void update() {
            loading();
        }
 
        public int getCount() {
            if(Bimp.tempSelectBitmap.size() == 9){
                return 9;
            }
            return (Bimp.tempSelectBitmap.size() + 1);
        }
 
        public Object getItem(int arg0) {
            return null;
        }
 
        public long getItemId(int arg0) {
            return 0;
        }
 
        public void setSelectedPosition(int position) {
            selectedPosition = position;
        }
 
        public int getSelectedPosition() {
            return selectedPosition;
        }
 
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_published_grida,
                        parent, false);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView
                        .findViewById(R.id.item_grida_image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
 
            if (position ==Bimp.tempSelectBitmap.size()) {
                holder.image.setImageBitmap(BitmapFactory.decodeResource(
                        getResources(), R.drawable.icon_addpic_unfocused));
                if (position == 9) {
                    holder.image.setVisibility(View.GONE);
                }
            } else {
                holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());
            }
 
            return convertView;
        }
 
        public class ViewHolder {
            public ImageView image;
        }
 
        Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case 1:
                    adapter.notifyDataSetChanged();
                    break;
                }
                super.handleMessage(msg);
            }
        };
 
        public void loading() {
            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        if (Bimp.max == Bimp.tempSelectBitmap.size()) {
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                            break;
                        } else {
                            Bimp.max += 1;
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                        }
                    }
                }
            }).start();
        }
    }
 
    public String getString(String s) {
        String path = null;
        if (s == null)
            return "";
        for (int i = s.length() - 1; i > 0; i++) {
            s.charAt(i);
        }
        return path;
    }
 
    protected void onRestart() {
        adapter.update();
        super.onRestart();
    }
 
    private static final int TAKE_PICTURE = 0x000001;
 
    public void photo() {
        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(openCameraIntent, TAKE_PICTURE);
    }
 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case TAKE_PICTURE:
            if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) {
                
                String fileName = String.valueOf(System.currentTimeMillis());
                Bitmap bm = (Bitmap) data.getExtras().get("data");
                FileUtils.saveBitmap(bm, fileName);
                
                ImageItem takePhoto = new ImageItem();
                takePhoto.setBitmap(bm);
                Bimp.tempSelectBitmap.add(takePhoto);
            }
            break;
        }
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            for(int i=0;i<PublicWay.activityList.size();i++){
                if (null != PublicWay.activityList.get(i)) {
                    PublicWay.activityList.get(i).finish();
                }
            }
            System.exit(0);
        }
        return true;
    }
 
}
 
 
復制代碼

下載地址 : 微信上傳圖片源碼

備份下載地址:http://download.csdn.net/detail/jdsjlzx/8486449

 

補充:

通過GridView仿微信動態添加本地圖片

 

此篇文章主要講述GridView控件實現添加本地圖片並顯示.主要是關於GridView控件的基本操作,通常可以通過自定義繼承BaseAdapter的適配器加載圖片,而下面講述的不是自定義的適配器,而是調用SimpleAdapter實現的.至於上傳發布與網絡交互此處不講述,后面文章會講!

一. 實現效果

    主要是通過點擊+從本地相冊中添加圖片,同時顯示圖片至GridView.點擊圖片可以進行刪除操作,同時界面中的發布EditView控件也很好看,不足之處在於+好沒有移動至最后,但原理相同.
      

二. 項目工程結構

 

 

三. 界面布局詳細代碼

    1.主界面activity_main.xml
    主要通過相對布局實現,第一部分是底部的TextView,中間是EditView和GridView相對布局,下面是兩個按鈕.同時EditView調用res/drawable-hdpi中的editview_shape.xml,GridView顯示的每張圖片通過griditem_addpic.xml實現.

復制代碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/container"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="com.example.suishoupaipublish.MainActivity"  
    tools:ignore="MergeRootFrame" >  
      
    <!-- 頂部添加文字 -->   
    <RelativeLayout    
        android:id="@+id/Layout_top"    
        android:orientation="horizontal"     
        android:layout_width="fill_parent"    
        android:layout_height="40dp"    
        android:layout_marginTop="5dp"  
        android:layout_alignParentTop="true"    
        android:gravity="center">    
        <TextView    
            android:layout_width="fill_parent"     
            android:layout_height="wrap_content"      
            android:textSize="25sp"  
            android:gravity="center"  
            android:text="發布信息" />   
    </RelativeLayout>    
    <!-- 底部按鈕 -->    
    <RelativeLayout    
        android:id="@+id/Layout_bottom"     
        android:layout_alignParentBottom="true"  
        android:layout_width="fill_parent"     
        android:layout_height="50dp"  
        android:gravity="center" >    
        <Button    
            android:id="@+id/button1"    
            android:layout_width="wrap_content"    
            android:layout_height="fill_parent"  
            android:textSize="20sp"  
            android:text="發布拍拍" />    
        <Button    
            android:id="@+id/button2"    
            android:layout_width="wrap_content"    
            android:layout_height="fill_parent"  
            android:layout_toRightOf="@+id/button1"  
            android:textSize="20sp"  
            android:text="取消發布" />  
    </RelativeLayout>    
    <!-- 顯示圖片 -->    
    <RelativeLayout    
        android:id="@+id/Content_Layout"       
        android:layout_width="fill_parent"     
        android:layout_height="fill_parent"    
        android:layout_above="@id/Layout_bottom"     
        android:layout_below="@id/Layout_top"      
        android:gravity="center">       
        <LinearLayout     
            android:layout_width="match_parent"    
            android:layout_height="match_parent"    
            android:orientation="vertical"  
            android:layout_alignParentBottom="true" >   
            <!-- 設置運行多行 設置圓角圖形 黑色字體-->  
            <EditText   
                android:id="@+id/editText1"  
                android:layout_height="120dp"  
                android:layout_width="fill_parent"  
                android:textColor="#000000"  
                android:layout_margin="12dp"  
                android:textSize="20sp"  
                android:hint="隨手說出你此刻的心聲..."  
                android:maxLength="500"  
                android:singleLine="false"  
                android:background="@drawable/editview_shape" />  
            <!-- 網格顯示圖片 行列間距5dp 每列寬度90dp -->  
            <GridView  
                android:id="@+id/gridView1"  
                android:layout_width="fill_parent"  
                android:layout_height="200dp"  
                android:layout_margin="10dp"  
                android:background="#EFDFDF"  
                android:horizontalSpacing="5dp"  
                android:verticalSpacing="5dp"  
                android:numColumns="4"  
                android:columnWidth="90dp"  
                android:stretchMode="columnWidth"  
                android:gravity="center" >  
            </GridView>  
            <TextView   
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:text="(友情提示:只能添加9張圖片,長按圖片可以刪除已添加圖片)"  
                android:gravity="center" />  
        </LinearLayout>  
    </RelativeLayout>  
      
</RelativeLayout>  
復制代碼

 2.顯示ImageView圖片布局griditem_addpic.xml

復制代碼
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:gravity="center"  
    android:descendantFocusability="blocksDescendants"  
    android:orientation="vertical" >  
    <RelativeLayout  
        android:layout_gravity="center"  
        android:layout_width="80dp"  
        android:layout_height="80dp"  
        android:orientation="vertical" >  
        <ImageView  
            android:layout_marginTop="10dp"  
            android:layout_marginRight="10dp"  
            android:id="@+id/imageView1"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:scaleType="fitXY"  
            android:src="@drawable/gridview_addpic" />  
    </RelativeLayout>  
</LinearLayout>  
復制代碼

 3.設置EditView控件圓角和顏色 editview_shape.xml

復制代碼
<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"   
    android:shape="rectangle"   
    android:padding="10dp">  
    <!-- 填充editView的顏色 -->  
    <soild android:color="#ffffff"/>  
    <!-- 設置圓角的弧度,radius半徑越大,editView的邊角越圓 -->  
    <corners   
        android:radius="15dp"  
        android:bottomRightRadius="15dp"  
        android:bottomLeftRadius="15dp"  
        android:topLeftRadius="15dp"    
        android:topRightRadius="15dp"/>  
    <stroke  
        android:color="#32CD32"    
        android:width="4px" />   
</shape>  
復制代碼

四. 代碼詳解

    它主要是思想如下:
    1.通過SimpleAdapter適配器實現實現加載圖片,在gridView1.setOnItemClickListener()點擊函數中響應不同操作.
    2.當點擊加號圖片(+)時,調用本地相冊通過Intent實現獲取圖片路徑存於字符串pathImage.
    3.獲取圖片路徑后在onResume中刷新圖片,通過GridView的setAdapter()和notifyDataSetChanged()()函數刷新加載圖片.
    4.點擊圖片時會獲取其position,通過dialog()函數彈出對話框提示是否刪除,通過remove實現刪除.
    具體代碼如下所示:
復制代碼
public class MainActivity extends Activity {  
  
    private GridView gridView1;              //網格顯示縮略圖  
    private Button buttonPublish;            //發布按鈕  
    private final int IMAGE_OPEN = 1;        //打開圖片標記  
    private String pathImage;                //選擇圖片路徑  
    private Bitmap bmp;                      //導入臨時圖片  
    private ArrayList<HashMap<String, Object>> imageItem;  
    private SimpleAdapter simpleAdapter;     //適配器  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        /* 
         * 防止鍵盤擋住輸入框 
         * 不希望遮擋設置activity屬性 android:windowSoftInputMode="adjustPan" 
         * 希望動態調整高度 android:windowSoftInputMode="adjustResize" 
         */  
        getWindow().setSoftInputMode(WindowManager.LayoutParams.  
                SOFT_INPUT_ADJUST_PAN);  
        //鎖定屏幕  
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
        setContentView(R.layout.activity_main);  
        //獲取控件對象  
        gridView1 = (GridView) findViewById(R.id.gridView1);  
  
        /* 
         * 載入默認圖片添加圖片加號 
         * 通過適配器實現 
         * SimpleAdapter參數imageItem為數據源 R.layout.griditem_addpic為布局 
         */  
        //獲取資源圖片加號  
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gridview_addpic);  
        imageItem = new ArrayList<HashMap<String, Object>>();  
        HashMap<String, Object> map = new HashMap<String, Object>();  
        map.put("itemImage", bmp);  
        imageItem.add(map);  
        simpleAdapter = new SimpleAdapter(this,   
                imageItem, R.layout.griditem_addpic,   
                new String[] { "itemImage"}, new int[] { R.id.imageView1});   
        /* 
         * HashMap載入bmp圖片在GridView中不顯示,但是如果載入資源ID能顯示 如 
         * map.put("itemImage", R.drawable.img); 
         * 解決方法: 
         *              1.自定義繼承BaseAdapter實現 
         *              2.ViewBinder()接口實現 
         *  參考 http://blog.csdn.net/admin_/article/details/7257901 
         */  
        simpleAdapter.setViewBinder(new ViewBinder() {    
            @Override    
            public boolean setViewValue(View view, Object data,    
                    String textRepresentation) {    
                // TODO Auto-generated method stub    
                if(view instanceof ImageView && data instanceof Bitmap){    
                    ImageView i = (ImageView)view;    
                    i.setImageBitmap((Bitmap) data);    
                    return true;    
                }    
                return false;    
            }  
        });    
        gridView1.setAdapter(simpleAdapter);  
          
        /* 
         * 監聽GridView點擊事件 
         * 報錯:該函數必須抽象方法 故需要手動導入import android.view.View; 
         */  
        gridView1.setOnItemClickListener(new OnItemClickListener() {  
            @Override  
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)  
            {  
                if( imageItem.size() == 10) { //第一張為默認圖片  
                    Toast.makeText(MainActivity.this, "圖片數9張已滿", Toast.LENGTH_SHORT).show();  
                }  
                else if(position == 0) { //點擊圖片位置為+ 0對應0張圖片  
                    Toast.makeText(MainActivity.this, "添加圖片", Toast.LENGTH_SHORT).show();  
                    //選擇圖片  
                    Intent intent = new Intent(Intent.ACTION_PICK,         
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);    
                    startActivityForResult(intent, IMAGE_OPEN);    
                    //通過onResume()刷新數據  
                }  
                else {  
                    dialog(position);  
                    //Toast.makeText(MainActivity.this, "點擊第"+(position + 1)+" 號圖片",   
                    //      Toast.LENGTH_SHORT).show();  
                }  
            }  
        });    
    }  
      
    //獲取圖片路徑 響應startActivityForResult    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
        super.onActivityResult(requestCode, resultCode, data);          
        //打開圖片    
        if(resultCode==RESULT_OK && requestCode==IMAGE_OPEN) {          
            Uri uri = data.getData();    
            if (!TextUtils.isEmpty(uri.getAuthority())) {    
                //查詢選擇圖片    
                Cursor cursor = getContentResolver().query(    
                        uri,    
                        new String[] { MediaStore.Images.Media.DATA },    
                        null,     
                        null,     
                        null);    
                //返回 沒找到選擇圖片    
                if (null == cursor) {    
                    return;    
                }    
                //光標移動至開頭 獲取圖片路徑    
                cursor.moveToFirst();    
                pathImage = cursor.getString(cursor    
                        .getColumnIndex(MediaStore.Images.Media.DATA));    
            }  
        }  //end if 打開圖片  
    }  
      
    //刷新圖片  
    @Override  
    protected void onResume() {  
        super.onResume();  
        if(!TextUtils.isEmpty(pathImage)){  
            Bitmap addbmp=BitmapFactory.decodeFile(pathImage);  
            HashMap<String, Object> map = new HashMap<String, Object>();  
            map.put("itemImage", addbmp);  
            imageItem.add(map);  
            simpleAdapter = new SimpleAdapter(this,   
                    imageItem, R.layout.griditem_addpic,   
                    new String[] { "itemImage"}, new int[] { R.id.imageView1});   
            simpleAdapter.setViewBinder(new ViewBinder() {    
                @Override    
                public boolean setViewValue(View view, Object data,    
                        String textRepresentation) {    
                    // TODO Auto-generated method stub    
                    if(view instanceof ImageView && data instanceof Bitmap){    
                        ImageView i = (ImageView)view;    
                        i.setImageBitmap((Bitmap) data);    
                        return true;    
                    }    
                    return false;    
                }  
            });   
            gridView1.setAdapter(simpleAdapter);  
            simpleAdapter.notifyDataSetChanged();  
            //刷新后釋放防止手機休眠后自動添加  
            pathImage = null;  
        }  
    }  
      
    /* 
     * Dialog對話框提示用戶刪除操作 
     * position為刪除圖片位置 
     */  
    protected void dialog(final int position) {  
        AlertDialog.Builder builder = new Builder(MainActivity.this);  
        builder.setMessage("確認移除已添加圖片嗎?");  
        builder.setTitle("提示");  
        builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
                imageItem.remove(position);  
                simpleAdapter.notifyDataSetChanged();  
            }  
        });  
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
                }  
            });  
        builder.create().show();  
    }  
  
}  
復制代碼

  同時需要在AndroidMainfest.xml中添加權限操作SD卡和網絡上傳至服務器.

<!-- 申明網絡權限  -->  
<uses-permission android:name="android.permission.INTERNET" />  
<!-- 申明權限 操作SD卡 -->    
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

五. 總結

    
    該文章需要注意一個地方:在使用SimpleAdapter適配器加載bmp圖片時,可能在GridView中不顯示.即HashMap中map.put("itemImage",bmp)不顯示圖片,而使用put裝入R.drawable.img卻能顯示.
    這時有兩種解決方法,一種是自定義繼承BaseAdapter的適配器實現;另一種方法則是如上所示通過ViewBinder()接口實現,感謝博主dmin_提供的方法.
    下載地址:http://download.csdn.net/detail/eastmount/8237429

 

下面再分享個圖片處理的博客。

 demo下載地址: 
        http://download.csdn.net/detail/eastmount/8432127
        http://pan.baidu.com/s/1c0kz3by

一. 項目結構及界面

 

        項目工程結構如下圖所示:

  
       個人還是比較欣賞這個布局的,其中MainActivity.java是主界面,ProcessActivity.java是圖像處理界面,而剩余五個java對應五個不同的圖像處理算法:EffectProcessImage(圖像特效)、FrameProcessImage(添加相框)、IncreaseProcessImage(圖像增強)、PersonProcessImage(圖像交互)和WatchProcessImage(查看圖片).
        同時Layout中有五個xml子布局,采用PopupWindow形式顯示五個不同的處理,同時該demo比較好移植,將文件依次復制過去並且MainActivity改成子活動,即可調用.
        其中部分界面如下:
       
                    MainActivity主界面                             添加圖片選項                         圖像處理子界面(底部五個) 

 

二. 圖像查看功能

        點擊主界面GridView中"+"添加圖片后,可以選擇從相冊添加或照相,加載圖片進行到處理界面后,點擊底部"查看"按鈕它會有選中的效果(圖標顏色變藍&背景顏色加深).同時PopupWindow彈出如下界面:


        其中點擊"旋轉"后提交的動態圖如下所示:
    
        參考以前我的博客:
             [Android] 拍照、截圖、保存並顯示在ImageView控件中        
             [Android] 使用Matrix矩陣類對圖像進行縮放、旋轉、對比度、亮度處理
             [Android] 觸屏setOnTouchListener實現圖片縮放、移動、繪制和添加水印
        其他效果如下,同時存在幾個問題:
            1.圖像繪圖保存我沒有做
            2.移動和縮放圖像,上傳的都是原圖大小
            3.添加水印功能感覺簡單就沒實現
   

三. 圖像增強功能

        圖像增強主要是通過3個進度條SeekBar實現,可以調節圖像的飽和度、亮度和色相.當飽和度等於0時就是黑白二值圖像,三個滑動條可以相互調節.如下圖所示:


        參考我的以前博客:
             [Android] 使用Matrix矩陣類對圖像進行縮放、旋轉、對比度、亮度處理
             [Android] 圖像處理整合之處理ColorMatrix和Intend傳遞路徑顯示圖像
             [Android] 底部菜單布局+PopupWindows實現彈出菜單功能(初級篇)
        同時需要注意一個問題: 設置滑動條時需要設置其區間,同時在彈出PopupWindow注意其寬度和不同手機的適屏處理.
        該部分算法處理代碼如下,每部分算法調用后返回值都是bitmap,然后調用imageShow.setImageBitmap(mbmp);直接顯示.

 

四. 圖像特效功能

        PopupWindow彈出如下界面,主要包括:懷舊、浮雕、光照、素描和銳化處理.

        其中特效效果和EffectProcessImage.java如下所示:
    
        參考我的博客:
             [Android] 通過Menu實現圖片懷舊、浮雕、模糊、光照和素描效果
             [Android] 圖像各種處理系列文章合集 (草稿 強推)
        同時算法中存在一些問題,一個就是效率不高,有好友建議C寫算法來調用,但是該文章僅是課堂作業,提供一種方法供大家參考.同時在圖像素描算法效果不好,銳化處理應該也不對(可參考博客),光照效果是從中心向邊沿映射,用戶可自己設置光照直徑和方向.
            

 

五. 圖像相框添加

        最后是圖像相框合成,如下圖所示.我采用的是照相后合成圖片:

    
        添加相框功能主要是通過兩張圖片的合成,我也見到過四張圖片分別合成相框的效果.但我更喜歡這種兩張圖片合成的,在assets文件夾中加載透明圖片.詳見:
         [Android] 給圖像添加相框、圓形圓角顯示圖片、圖像合成知識

 

六. 總結及感想

        該處理過程中的底部五個按鈕切換背景和顏色詳見代碼ProcessActivity.Java,主要是通切換兩張圖片和背景實現.如果每個界面顯示不同內容,建議使用Fragment滑動實現.參考:
        [Android] 使用Include布局+Fragment滑動切換屏幕
        [Android] 通過GridView仿微信動態添加本地圖片
        隨手拍我還是比較滿意的,其中后台數據庫我們使用的是新浪SAE做的,同時ListView刷新拍拍也非常不錯!效果如下:

     
        上傳的demo我把發布功能刪除了,它是通過線程和Process進度條來上傳多張圖片的.同時ListView可以刷新最近發布消息,點擊圖片可以查看大圖.可以對發布的拍拍進行評論、關注、點贊等操作.
        但是由於整個項目是大家一起完成,包括網絡、數據庫(SAE)、ListView(引用開源),所以只能共享我做的那部分.抱歉~如果有時間可以寫些關於Android網絡方面的文章,包括如何上傳圖片(URL鏈接存儲在雲盤中)、獲取數據庫內容等操作.
        PS:最近其實挺煩躁的,一方面這學期確實忙成狗了,寒假也還有個作業.但在火車上我還是挺平靜的——聽着調皮的鋼琴曲,看着窗外流逝的繁華,不知道從什么時候自己已經喜歡上了這種26小時獨處的感覺.感受頗多啊!另一個挺令我心煩的就是回家后不久就去做了個手指的小手術,今天寫完這篇文章,心情稍微好點!驀然回首,突然發現這輩子我最對不住的人居然是我自己,希望來年對自己有點,尤其是對自己的身體.同時有機會,找份實習吧!但幸運的是,每每分享一些博客、完成一些項目后都能給我帶來一份快樂.
        希望文章對大家有所幫助~
        最后用最近看的《老人與海》結束這篇文章:
        生活總是讓我們遍體鱗傷,但到后來,那些受傷的地方一定會變成我們最強壯的地方.
      (By:Eastmount 2015-2-7 夜11點    http://blog.csdn.net/eastmount/

 Android仿微信圖片上傳,可以選擇多張圖片,縮放預覽,拍照上傳等


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM