Android模仿发说说 完美版 无bug 可以本地获取相机,相册 删除,点击进入滑动 ui更新

现在直接贴代码,这个demo代码很少,2个activity之间的回调跳转时必须的,图中的小图标怎么找了,这个是最直接的功能,希望能帮助到和我一样的小白: property_repair.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"  
  android:background="#ffffff"  
  tools:ignore="MergeRootFrame" >  

      <Button   
          android:id="@+id/button1"  
          android:layout_width="100dp"  
          android:layout_height="30dp"  
          android:text="提交问题"  
          android:gravity="center"  
          android:textColor="#1Eb2ff"  
          android:background="@null"  
          android:textSize="16sp"  
          android:layout_alignParentRight="true"  
          />  

  <RelativeLayout    
    android:id="@+id/Content_Layout"       
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent"    
    android:layout_below="@id/button1"    
    android:gravity="center">       

    <LinearLayout     
      android:layout_width="match_parent"    
      android:layout_height="match_parent"    
      android:orientation="vertical"  
      android:background="#ffffff"  
      android:layout_alignParentBottom="true" >   

      <EditText   
        android:id="@+id/editText1"  
        android:layout_height="120dp"  
        android:layout_width="fill_parent"  
        android:textColor="#1eb2ff"  
        android:gravity="top"  
        android:textSize="15sp"  
        android:hint="请说明你的问题..."  
        android:maxLength="500"  
        android:singleLine="false"  
        android:background="#ffffff"  
     />  
      <GridView  
          android:id="@+id/Scrollgridview"  
          android:layout_width="fill_parent"  
          android:layout_height="180dp"  
          android:layout_marginLeft="28dp"  
          android:columnWidth="90dp"  
          android:gravity="center"  
          android:background="#ffffff"  
          android:horizontalSpacing="5dp"  
          android:listSelector="@android:color/transparent"  
          android:numColumns="3"  
          android:scrollbars="none"  
          android:stretchMode="columnWidth"  
          android:verticalSpacing="10dp" />  

    </LinearLayout>  
  </RelativeLayout>  

</RelativeLayout>

activity_photo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/LinearLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  

    <LinearLayout   
        android:id="@+id/linear_layout_fanhui"  
        android:layout_width="match_parent"  
        android:layout_marginTop="20dp"  
        android:layout_height="23dp"  

        >  

        <Button  
            android:id="@+id/fanhui"  
            android:layout_width="20dp"  
            android:layout_marginLeft="30dp"  
            android:layout_height="match_parent"  
            android:layout_gravity="center_vertical"  
            android:background="@drawable/back"  
            android:paddingLeft="5dp"  
            android:textSize="15sp" />  
    </LinearLayout>  


    <android.support.v4.view.ViewPager  
        android:id="@+id/viewpager"  
        android:layout_width="match_parent"  
        android:layout_height="500dp" >  
    </android.support.v4.view.ViewPager>  

    <Button   
        android:id="@+id/deldete"  
        android:layout_width="wrap_content"  
        ndroid:layout_height="match_parent"          
        android:layout_gravity="center_horizontal"  
        android:background="@drawable/delet"  
        />  

</LinearLayout>

activity_selectimg.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:background="#ffffff"  
    android:orientation="vertical" >  

                <HorizontalScrollView  
                    android:id="@+id/selectimg_horizontalScrollView"  
                    android:layout_width="fill_parent"  
                    android:layout_height="wrap_content"  
                    android:paddingLeft="10dp"  
                    android:paddingRight="10dp" >  

                    <LinearLayout  
                        android:layout_width="fill_parent"  
                        android:layout_height="fill_parent"  
                        android:orientation="horizontal" >  

                        <GridView  
                            android:id="@+id/noScrollgridview"  
                            android:layout_width="fill_parent"  
                            android:layout_height="fill_parent"  
                            android:fastScrollEnabled="true"  
                            android:focusable="true"  
                            android:gravity="center"  
                            android:horizontalSpacing="0dp"  
                            android:scrollbars="none"  
                            android:scrollingCache="false"  
                            android:stretchMode="spacingWidthUniform"  
                            android:verticalSpacing="1dp" >  
                        </GridView>  
                    </LinearLayout>  
                </HorizontalScrollView>  

</LinearLayout>

item_popupwindows.xml

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

    <LinearLayout  
        android:id="@+id/ll_popup"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:background="#ffffff"  
        android:orientation="vertical" >  

        <TextView  
            android:layout_width="match_parent"  
            android:layout_height="1dp"  
            android:background="#ff495a" />  

        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="55dp"  
            android:orientation="horizontal" >  

            <Button  
                android:id="@+id/item_popupwindows_camera"  
                android:layout_width="match_parent"  
                android:layout_height="55dp"  
                android:background="@drawable/bt_nobgd_top_button_left_right_11"  
                android:text="拍照"  
                android:textColor="#585858"  
                android:textSize="18sp" />  
        </LinearLayout>  

        <TextView  
            android:layout_width="match_parent"  
            android:layout_height="1px"  
            android:layout_marginLeft="80dp"  
            android:background="#f2f2f2" />  

        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="55dp"  
            android:orientation="horizontal" >  

            <Button  
                android:id="@+id/item_popupwindows_Photo"  
                android:layout_width="match_parent"  
                android:layout_height="55dp"  
                android:background="@drawable/bt_nobgd_top_button_left_right_11"  
                android:text="从相册中选取"  
                android:textColor="#585858"  
                android:textSize="18sp" />  
        </LinearLayout>  

        <TextView  
            android:layout_width="match_parent"  
            android:layout_height="2dp"  
            android:background="#f3f3f3" />  

        <Button  
            android:id="@+id/item_popupwindows_cancel"  
            android:layout_width="match_parent"  
            android:layout_height="55dp"  
            android:background="@drawable/bt_nobgd_top_button_left_right_11"  
            android:text="取消"  
            android:textColor="#585858"  
            android:textSize="18sp" />  
    </LinearLayout>  

</RelativeLayout>

item_published_grida.xml

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

        <ImageView  
            android:id="@+id/item_grida_image"  
            android:layout_width="80dp"  
            android:layout_height="80dp"  
                 android:scaleType="fitXY"  
             />  

</RelativeLayout>

当然权限是少不了的:

<uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.CAMERA" />  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

<uses-feature android:name="android.hardware.camera" />  
<uses-feature android:name="android.hardware.camera.autofocus" />

下面是程序: PhotoActivity.Java

package com.dynamic;  

import java.util.ArrayList;  
import java.util.List;  

import android.app.Activity;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.os.Bundle;  
import android.support.v4.view.PagerAdapter;  
import android.support.v4.view.ViewPager;  
import android.support.v4.view.ViewPager.OnPageChangeListener;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.view.ViewGroup.LayoutParams;  
import android.widget.Button;  
import android.widget.ImageView;  




public class PhotoActivity extends Activity {  
    public  List<Bitmap> bitmap = new ArrayList<Bitmap>();  
    private ArrayList<View> listViews ;  
    private ViewPager pager;  
    private MyPageAdapter adapter;  
    private Button fanhui;  
    private Button deldete;  
    private int index = 0;    
    private MainActivity property_repair;  

    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_photo);  
                deldete=(Button) findViewById(R.id. deldete);  
            /** 
             * 删除照片 
             */  
            deldete.setOnClickListener(new OnClickListener() {  

                @Override  
                public void onClick(View v) {  

                    if (listViews.size() > 0) {   
                        listViews.remove(index);    
                        MainActivity.bmp.remove(index);  
                        adapter.notifyDataSetChanged();    
                        }  
            }  
        });  

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

            @Override  
            public void onClick(View v) {  
                finish();  
            }  
        });  
        pager = (ViewPager) findViewById(R.id.viewpager);  
        pager.setOnPageChangeListener(pageChangeListener);  

        for (int i = 0; i < property_repair.bmp.size(); i++) {  
            initListViews(property_repair.bmp.get(i));  
        }  
        adapter = new MyPageAdapter(listViews);// 构造adapter  
        pager.setAdapter(adapter); //设置适配器  
        Intent intent = getIntent();  
        int id = intent.getIntExtra("ID", 0);  
        pager.setCurrentItem(id);  
    }  

    private void initListViews(Bitmap bm) {  

        if (listViews == null)  
            listViews = new ArrayList<View>();  
        final ImageView img = new ImageView(this);// 构造textView对象  

        img.setImageBitmap(bm);  
        img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  

        // 添加view  
        listViews.add(img);  

    }  

    private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {  

        public void onPageSelected(int arg0) {  
            // 页面选择响应函数  
                index = arg0;  
        }  

        public void onPageScrolled(int arg0, float arg1, int arg2) {// 滑动中。。。  

        }  

        public void onPageScrollStateChanged(int arg0) {// 滑动状态改变  

        }  
    };  

    class MyPageAdapter extends PagerAdapter {  

        private ArrayList<View> listViews;// content  

        private int size;// 页数  

        public MyPageAdapter(ArrayList<View> listViews) {// 构造函数  
                                                            // 初始化viewpager的时候给的一个页面  
            this.listViews = listViews;  
            size = listViews == null ? 0 : listViews.size();  
        }  

        public void setListViews(ArrayList<View> listViews) {// 自己写的一个方法用来添加数据  
            this.listViews = listViews;  
            size = listViews == null ? 0 : listViews.size();  
        }  

        public int getCount() {// 返回数量  
             if (listViews != null && listViews.size() > 0) {    
                    return listViews.size();    
                } else {    
                    return 0;    
                }    
        }  

        public int getItemPosition(Object object) {  
            return POSITION_NONE;  
        }  

        public void destroyItem(ViewGroup container, int position, Object object) {    
            container.removeView((View) object);    
            if(listViews.size()==0){  

                finish();  
            }  
        }    

        public void finishUpdate(View arg0) {  
        }  

        public Object instantiateItem(ViewGroup container, int position) {    
            container.addView(listViews.get(position));    
            return listViews.get(position);    
        }    

        public boolean isViewFromObject(View arg0, Object arg1) {  
            return arg0 == arg1;  
        }  

    }  


}

MainActivity.java

package com.dynamic;  

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.util.ArrayList;  
import java.util.List;  

import android.app.Activity;  
import android.content.Context;  
import android.content.Intent;  
import android.database.Cursor;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.graphics.RectF;  
import android.graphics.drawable.BitmapDrawable;  
import android.graphics.drawable.Drawable;  
import android.net.Uri;  
import android.os.Bundle;  
import android.os.Environment;  
import android.provider.MediaStore;  
import android.view.Gravity;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.view.WindowManager.LayoutParams;  
import android.view.inputmethod.InputMethodManager;  
import android.widget.AdapterView;  
import android.widget.AdapterView.OnItemClickListener;  
import android.widget.BaseAdapter;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.GridView;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.widget.PopupWindow;  
import android.widget.Toast;  


public class MainActivity extends Activity implements OnItemClickListener {  

    private GridView gridview;  
    private GridAdapter adapter;  
    private float dp;  
    public  static List<Bitmap> bmp = new ArrayList<Bitmap>();  
    public  static List<String> drr = new ArrayList<String>();  
    private     LinearLayout ll_popup ;  
    private Button button1;// 发布按钮  


    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.property_repair);  
        Init();  


        final EditText editText1 = (EditText) findViewById(R.id.editText1);  
        button1 = (Button) findViewById(R.id.button1);  
        button1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                if (editText1.getText().toString().length() == 0) {  
                    Toast.makeText(MainActivity.this, "请简述详情",  
                            Toast.LENGTH_SHORT).show();  
                } else  
                {  
                    Toast.makeText(MainActivity.this, "提交成功",Toast.LENGTH_SHORT).show();  
                                    finish();  
                }  
            }  
        });  
    }  
    List<String> urList = new ArrayList<String>();  
        public void Init() {  
            dp = getResources().getDimension(R.dimen.dp);  
            gridview = (GridView) findViewById(R.id.Scrollgridview);  
            gridviewInit();  
        }  


    public void gridviewInit() {  

                adapter = new GridAdapter(this,bmp);  
                adapter.setSelectedPosition(0);  
                gridview.setAdapter(adapter);  
                adapter.notifyDataSetChanged();  
                gridview.setOnItemClickListener(this);  

    }  


        protected void onPause() {  
            super.onPause();  
        }  

    private class GridAdapter extends BaseAdapter {  
        private LayoutInflater listContainer;  
        private int selectedPosition = -1;  
        private   List<Bitmap> bmps=new ArrayList<Bitmap>();  
        private class ViewHolder {  
            public ImageView image;  
        }  
        private GridAdapter(Context context, List<Bitmap> bmp) {  
            listContainer = LayoutInflater.from(context);  
            bmps=bmp;  
        }  

        public int getCount() {  
            return bmps.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) {  
            final int sign = position;  
            ViewHolder holder = null;  
            if (convertView == null) {  
                holder = new ViewHolder();  
                convertView = listContainer.inflate(  
                        R.layout.item_published_grida, null);  
                holder.image = (ImageView) convertView  
                        .findViewById(R.id.item_grida_image);  

                convertView.setTag(holder);  
            } else {  
                holder = (ViewHolder) convertView.getTag();  
            }  

            if (position == bmps.size()) {  
                holder.image.setImageBitmap(BitmapFactory.decodeResource(  
                        getResources(), R.drawable.icon_addpic_unfocused));  
                if (position == 6) {  
                    holder.image.setVisibility(View.GONE);  
                }  
            } else {  
                holder.image.setImageBitmap(bmps.get(position));  
            }  
            return convertView;  
        }  
    }  
    private class PopupWindows extends PopupWindow {  

        private PopupWindows(Context mContext, View parent) {  

            View view = View.inflate(mContext, R.layout.item_popupwindows, null);  
            ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);  
            setWidth(LayoutParams.FILL_PARENT);  
            setHeight(LayoutParams.FILL_PARENT);  
            setFocusable(true);  
            setOutsideTouchable(true);  
            setContentView(view);  
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);  
            update();  

            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);  


            bt1.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                    photo();  
                    dismiss();  
                }  
            });  
            bt2.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
                        startActivityForResult(i, RESULT_LOAD_IMAGE);  
                            dismiss();  
                }  
            });  

            bt3.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                        dismiss();  
                }  
            });  
        }  
    }  
    private static final int TAKE_PICTURE = 0;  
    private static final int RESULT_LOAD_IMAGE = 1;  
    private static final int UI=2;  
    private String path = "";  
    private Uri mOutPutFileUri;  
    String picturePath;  
    private void photo() {  

          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
          //文件夹aaaa  
          String path = Environment.getExternalStorageDirectory().toString()+"/elife/img";  
          File path1 = new File(path);  
          if(!path1.exists()){  
           path1.mkdirs();  
          }  
          File file = new File(path1,System.currentTimeMillis()+".jpg");  
          mOutPutFileUri = Uri.fromFile(file);  
          picturePath=file.getAbsolutePath();  
          intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutPutFileUri);  
          startActivityForResult(intent, TAKE_PICTURE);  
    }  
    private void photoview() {  
                Intent pc=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
                startActivityForResult(pc, RESULT_LOAD_IMAGE);  
    }  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);  
        switch (requestCode) {  
        // 拍照  
        case TAKE_PICTURE:  
            if (resultCode==RESULT_OK) {  
                startPhotoZoom(picturePath);  
            }  

            break;  
        // 相册  
        case RESULT_LOAD_IMAGE:  
            if (resultCode==RESULT_OK) {  

                Uri uri=data.getData();    
                String[] files={MediaStore.Images.Media.DATA};    
                Cursor c=this.getContentResolver().query(uri, files, null, null, null);    
                c.moveToFirst();    
                int ii=c.getColumnIndex(files[0]);    
                path=c.getString(ii);  
                c.close();    
                startPhotoZoom(path);  

            }  


            break;  

        }  
    }  
    private Bitmap getLoacalBitmap(String url) {  
        try {  
            FileInputStream fis = new FileInputStream(url);  
            return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片  

        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
    private Bitmap createFramedPhoto(int x, int y, Bitmap image, float outerRadiusRat) {  
        Drawable imageDrawable = new BitmapDrawable(image);  
        Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);  
        Canvas canvas = new Canvas(output);  
        RectF outerRect = new RectF(0, 0, x, y);  
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
        paint.setColor(Color.RED);  
        canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint);  
//      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
        imageDrawable.setBounds(0, 0, x, y);  
        canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);  
        imageDrawable.draw(canvas);  
        canvas.restore();  

        return output;  
    }  
    private void startPhotoZoom(String uri) {  
            drr.add(uri);  
            Bitmap bitmap =getLoacalBitmap(drr.get(drr.size()-1 ));  
                    bmp.add(bitmap);  
                    gridviewInit();  

    }  
        protected void onDestroy() {  
                    bmp.clear();  
                    drr.clear();  
                super.onDestroy();  
    }  

        @Override  
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).  

            hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  


            if (arg2 == bmp.size()) {  
            String sdcardState = Environment.getExternalStorageState();  
            if (Environment.MEDIA_MOUNTED.equals(sdcardState)) {  
                if(bmp.size()<5){  
                new PopupWindows(MainActivity.this, gridview);  
                }  
                else{  
                    Toast.makeText(getApplicationContext(), "一次只能传5张", Toast.LENGTH_LONG).show();  
                }  
            }   

            else {  
                Toast.makeText(getApplicationContext(), "sdcard已拔出,不能选择照片", Toast.LENGTH_SHORT).show();  
            }  
        }   

            else   
            {  
                Intent intent = new Intent(MainActivity.this,PhotoActivity.class);  
                intent.putExtra("ID", arg2);  
                startActivity(intent);  
            }  


    }  
        @Override  
        protected void onResume() {  
            adapter.notifyDataSetChanged();  
                super.onResume();  
        }  

}

效果:

输入图片说明 输入图片说明 输入图片说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值