1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.camera.ui;
import com.android.camera.R;
import com.android.camera.Util;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.PopupWindow;
// A popup window that contains a big thumbnail and a list of apps to share.
public class SharePopup extends PopupWindow implements View.OnClickListener,
View.OnTouchListener {
private Context mContext;
private Uri mUri;
// The maximum width of the thumbnail in landscape orientation.
private final float mImageMaxWidthLandscape;
// The maximum height of the thumbnail in landscape orientation.
private final float mImageMaxHeightLandscape;
// The maximum width of the thumbnail in portrait orientation.
private final float mImageMaxWidthPortrait;
// The maximum height of the thumbnail in portrait orientation.
private final float mImageMaxHeightPortrait;
private ImageView mThumbnail;
private int mBitmapWidth, mBitmapHeight;
private RotateLayout mRotateLayout;
public SharePopup(Activity activity, Uri uri, Bitmap bitmap, int orientation, View anchor) {
super(activity);
// Initailize variables
mContext = activity;
mUri = uri;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.share_popup, null, false);
// This is required because popup window is full screen.
contentView.setOnTouchListener(this);
mRotateLayout = (RotateLayout) contentView.findViewById(R.id.rotate_layout);
mThumbnail = (ImageView) contentView.findViewById(R.id.expanded_thumbnail);
mThumbnail.setImageBitmap(bitmap);
mThumbnail.setOnClickListener(this);
mBitmapWidth = bitmap.getWidth();
mBitmapHeight = bitmap.getHeight();
Resources res = mContext.getResources();
mImageMaxWidthLandscape = res.getDimension(R.dimen.share_image_max_width_landscape);
mImageMaxHeightLandscape = res.getDimension(R.dimen.share_image_max_height_landscape);
mImageMaxWidthPortrait = res.getDimension(R.dimen.share_image_max_width_portrait);
mImageMaxHeightPortrait = res.getDimension(R.dimen.share_image_max_height_portrait);
// Initialize popup window
setWidth(WindowManager.LayoutParams.MATCH_PARENT);
setHeight(WindowManager.LayoutParams.MATCH_PARENT);
setBackgroundDrawable(new ColorDrawable());
setContentView(contentView);
setOrientation(orientation);
// Initialize view location
int location[] = new int[2];
anchor.getLocationOnScreen(location);
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
MarginLayoutParams params = (MarginLayoutParams) mRotateLayout.getLayoutParams();
params.topMargin = location[1] / 2;
params.rightMargin = (metrics.widthPixels - location[0] - anchor.getWidth()) / 2;
mRotateLayout.setLayoutParams(params);
// Start animation
Animation fadeIn = AnimationUtils.loadAnimation(mContext, R.anim.grow_fade_in_from_right);
mRotateLayout.startAnimation(fadeIn);
}
public void setOrientation(int orientation) {
// Calculate the width and the height of the thumbnail.
float width, height;
if (orientation == 90 || orientation == 270) {
width = mImageMaxWidthPortrait;
height = mImageMaxHeightPortrait;
} else {
width = mImageMaxWidthLandscape;
height = mImageMaxHeightLandscape;
}
LayoutParams params = mThumbnail.getLayoutParams();
if (width * mBitmapHeight > height * mBitmapWidth) {
params.width = Math.round(mBitmapWidth * height / mBitmapHeight);
params.height = Math.round(height);
} else {
params.width = Math.round(width);
params.height = Math.round(mBitmapHeight * params.width / mBitmapWidth);
}
mThumbnail.setLayoutParams(params);
mRotateLayout.setOrientation(orientation);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.expanded_thumbnail:
Util.viewUri(mUri, mContext);
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
dismiss();
return true;
}
return false;
}
}
|