用PopWindow仿iOS照片上传的dialog

来源:互联网 发布:消防联动编程规范 编辑:程序博客网 时间:2024/06/06 07:16

很多时候 产品经理要求Android 要照着iOS,本人写了一个仿iOS的照片上传,

如有问题可以与本菜鸟联系 QQ:2428566234技术讨论群:387648673


public class MainActivity extends FragmentActivity implements View.OnClickListener {


    private TextView takePhotosTv;
    private  PopupWindow popWindow;
    private TextView takePicturesTv,cameraTv,cancleTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        takePhotosTv =(TextView) findViewById(R.id.tv_main_takephotos);
        takePhotosTv.setOnClickListener(this);

    }


    private void showPopWindow() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View  view = inflater.inflate(R.layout.pupwindow, null);
        popWindow =  new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        popWindow.setFocusable(true);
        //设置popWindow可点击
        popWindow.setTouchable(true);
         //为popWindow设置背景
        ColorDrawable cd = new ColorDrawable(Color.parseColor("#FFFFFFFF"));
        popWindow.setBackgroundDrawable(cd);
        popWindow.setAnimationStyle(R.style.AnimBottom);
        popWindow.showAtLocation(takePhotosTv, Gravity.BOTTOM,0,0);
        takePicturesTv = (TextView) view.findViewById(R.id.tv_takepictures);
        cameraTv = (TextView) view.findViewById(R.id.tv_camera);
        cancleTv = (TextView) view.findViewById(R.id.tv_cancle);
        takePicturesTv.setOnClickListener(this);
        cameraTv.setOnClickListener(this);
        cancleTv.setOnClickListener(this);

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_main_takephotos:
                showPopWindow();
                break;
            case R.id.tv_takepictures:
                Toast.makeText(MainActivity.this,"拍照",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_camera:
                Toast.makeText(MainActivity.this,"相册",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_cancle:
                popWindow.dismiss();
                Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_LONG).show();
                break;
        }
    }
}

看看布局 activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     tools:context=".MainActivity">


    <TextView
        android:id="@+id/tv_main_takephotos"
        android:text="上传照片"
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        android:textSize="22sp"
        android:gravity="center"
        android:layout_height="50dp" />

</RelativeLayout>

pupwindow.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:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70.5dp"
        android:layout_margin="5dp"
        android:background="@drawable/dialog_white_bg"
        android:orientation="vertical">


        <TextView
            android:id="@+id/tv_camera"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:gravity="center"
            android:text="相册"
            android:textSize="17sp" />


        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#cccccc">
        </View>


        <TextView
            android:id="@+id/tv_takepictures"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:gravity="center"
            android:text="拍照"
            android:textSize="17sp" />


    </LinearLayout>
   <TextView
        android:id="@+id/tv_cancle"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_margin="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/dialog_white_bg"
        android:gravity="center"
        android:text="取消"
        android:textSize="17sp" />
</LinearLayout>

Style 文件

<resources>    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->    </style>    <style name="AnimBottom" parent="@android:style/Animation">        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>    </style></resources>

动画效果在res 新建anim 文件

新建push_bottom_in   push_bottom_out  都是以下文件

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


    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />


    <alpha
        android:duration="200"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>

拍照的背景drawable 文件

dialog_white_bg.xml代码如下<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:state_pressed="true"><shape>

            <!-- 描边 -->
            <stroke android:width="0.5dp" android:color="#cccccc" />

            <solid android:color="#ffffff" />
            <!-- 圆角 -->
            <corners android:radius="6dp" />

        </shape></item>
    <item android:state_focused="true"><shape>


            <!-- 描边 -->
            <stroke android:width="0.5dp" android:color="#cccccc" />

            <solid android:color="#ffffff" />
            <!-- 圆角 -->
            <corners android:radius="6dp" />

        </shape></item>
    <item><shape>

            <!-- 描边 -->
            <stroke android:width="0.5dp" android:color="#cccccc" />
            <solid android:color="#ffffff" />
            <!-- 圆角 -->
            <corners android:radius="6dp" />

        </shape></item>

</selector>


2 0