Java 具有随机图像的Gridview
如何使用图像视图随机图像制作Java 具有随机图像的Gridview,java,android,Java,Android,如何使用图像视图随机图像制作Gridview动画Gridviewanimation已经成功了,那么我如何放置随机图像呢?很抱歉,如果我更新此,使用限制显示的gridview可以吗 它的工作原理如下:当我们进入有趣的类时,它将显示带有随机图像的gridview <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Fun" xmlns:tools="http://
Gridview
动画Gridview
animation已经成功了,那么我如何放置随机图像呢?很抱歉,如果我更新此,使用限制显示的gridview可以吗
它的工作原理如下:当我们进入有趣的类时,它将显示带有随机图像的gridview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Fun"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/material_deep_teal_500"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.app.aditya.pecs.Fun">
<GridView
android:id="@+id/gridFu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:columnWidth="50dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
android:focusable="true"
android:clickable="true"/>
<ImageView
android:id="@+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
android:visibility="invisible" />
网格视图XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:weightSum="1">
<ImageView
android:id="@+id/grid_image"
android:layout_width="150dp"
android:layout_height="150dp">
</ImageView>
趣味班
private GridView gridFu;
private Animator mCurrentAnimator;
private int mShortAnimationDuration;
// Create Array thumbs resource id's:
private int Fun[] = { R.drawable.cherry, R.drawable.dragonfruit,
R.drawable.mango, R.drawable.orange,};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun);
// Initialize the variables:
gridFu = (GridView) findViewById(R.id.gridFu);
// Set an Adapter to the ListView
gridFu.setAdapter(new ImageAdapter(this));
// Set on item click listener to the ListView
gridFu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// Display the zoomed in image in full screen
zoomImageFromThumb(v, Fun[pos]);
}
});
// Set the Animation time form the android defaults
mShortAnimationDuration = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
// Create an Adapter Class extending the BaseAdapter
class ImageAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageAdapter(Fun activity) {
// TODO Auto-generated constructor stub
layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return Fun.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.grid_view, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.grid_image);
// Set the views in the layout
iv.setBackgroundResource(Fun[pos]);
return listItem;
}
}
private void zoomImageFromThumb(final View thumbView, int imageResId) {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}
final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
expandedImageView.setImageResource(imageResId);
final Rect startBounds = new Rect();
final Rect finalBounds = new Rect();
final Point globalOffset = new Point();
thumbView.getGlobalVisibleRect(startBounds);
findViewById(R.id.PhotoFruit).getGlobalVisibleRect(finalBounds,
globalOffset);
startBounds.offset(-globalOffset.x, -globalOffset.y);
finalBounds.offset(-globalOffset.x, -globalOffset.y);
float startScale;
if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds
.width() / startBounds.height()) {
// Extend start bounds horizontally
startScale = (float) startBounds.height() / finalBounds.height();
float startWidth = startScale * finalBounds.width();
float deltaWidth = (startWidth - startBounds.width()) / 2;
startBounds.left -= deltaWidth;
startBounds.right += deltaWidth;
} else {
// Extend start bounds vertically
startScale = (float) startBounds.width() / finalBounds.width();
float startHeight = startScale * finalBounds.height();
float deltaHeight = (startHeight - startBounds.height()) / 2;
startBounds.top -= deltaHeight;
startBounds.bottom += deltaHeight;
}
thumbView.setAlpha(0f);
expandedImageView.setVisibility(View.VISIBLE);
expandedImageView.setPivotX(0f);
expandedImageView.setPivotY(0f);
AnimatorSet set = new AnimatorSet();
set.play(
ObjectAnimator.ofFloat(expandedImageView, View.X,
startBounds.left, finalBounds.left))
.with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
startBounds.top, finalBounds.top))
.with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X,
startScale, 1f))
.with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y,
startScale, 1f));
set.setDuration(mShortAnimationDuration);
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mCurrentAnimator = null;
}
@Override
public void onAnimationCancel(Animator animation) {
mCurrentAnimator = null;
}
});
set.start();
mCurrentAnimator = set;
final float startScaleFinal = startScale;
expandedImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}
AnimatorSet set = new AnimatorSet();
set.play(
ObjectAnimator.ofFloat(expandedImageView, View.X,
startBounds.left))
.with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
startBounds.top))
.with(ObjectAnimator.ofFloat(expandedImageView,
View.SCALE_X, startScaleFinal))
.with(ObjectAnimator.ofFloat(expandedImageView,
View.SCALE_Y, startScaleFinal));
set.setDuration(mShortAnimationDuration);
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
thumbView.setAlpha(1f);
expandedImageView.setVisibility(View.GONE);
mCurrentAnimator = null;
}
@Override
public void onAnimationCancel(Animator animation) {
thumbView.setAlpha(1f);
expandedImageView.setVisibility(View.GONE);
mCurrentAnimator = null;
}
});
set.start();
mCurrentAnimator = set;
}
});
}
}
private-GridView-gridFu;
私人动画师麦柯伦塔尼玛;
私有整数mShortAnimationDuration;
//创建阵列拇指资源id:
private int Fun[]={R.drawable.cherry,R.drawable.dragonfruit,
R.drawable.mango,R.drawable.orange,};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun);
//初始化变量:
gridFu=(GridView)findviewbyd(R.id.gridFu);
//将适配器设置为ListView
setAdapter(新的ImageAdapter(this));
//在项目上设置单击ListView的侦听器
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(适配器视图父视图、视图v、内部位置、,
长id){
//全屏显示放大的图像
zoomImageFromThumb(v,Fun[pos]);
}
});
//从android默认设置设置动画时间
mShortAnimationDuration=getResources().getInteger(
android.R.integer.config_(短时间);
}
//创建扩展BaseAdapter的适配器类
类ImageAdapter扩展BaseAdapter{
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
公共图像适配器(趣味活动){
//TODO自动生成的构造函数存根
layoutInflater=(layoutInflater)活动
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
public int getCount(){
返回Fun.length;
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//充气项目布局并设置视图
查看列表项=转换视图;
int pos=位置;
如果(listItem==null){
listItem=LayoutFlater.inflate(R.layout.grid_视图,空);
}
//初始化布局中的视图
ImageView iv=(ImageView)listItem.findViewById(R.id.grid\u image);
//在布局中设置视图
iv.挫折资源(乐趣[pos]);
返回列表项;
}
}
私有void zoomImageFromThumb(最终视图thumbView,int-imageResId){
if(mCurrentAnimator!=null){
mCurrentAnimator.cancel();
}
最终图像视图展开图像视图=(图像视图)findViewById(R.id.expanded_图像);
expandedImageView.setImageResource(imageResId);
final Rect startBounds=新Rect();
final Rect finalBounds=新Rect();
最终点全局偏移=新点();
thumbView.getGlobalVisibleRect(startBounds);
findViewById(R.id.PhotoFruit)。getGlobalVisibleRect(finalBounds,
全局偏移);
起始边界偏移(-globalOffset.x,-globalOffset.y);
最终边界偏移(-globalOffset.x,-globalOffset.y);
浮标;
如果((浮动)finalBounds.width()/finalBounds.height()>(浮动)起始边界
.width()/startBounds.height()){
//水平扩展起始边界
startScale=(float)startBounds.height()/finalBounds.height();
float startWidth=startScale*finalBounds.width();
float deltaWidth=(startWidth-startBounds.width())/2;
startBounds.left-=三角洲宽度;
startBounds.right+=三角洲宽度;
}否则{
//垂直扩展起始边界
startScale=(float)startBounds.width()/finalBounds.width();
float startHeight=startScale*finalBounds.height();
float deltahheight=(startHeight-startBounds.height())/2;
startBounds.top-=deltaHeight;
startBounds.bottom+=deltaHeight;
}
thumbView.setAlpha(0f);
expandedImageView.setVisibility(View.VISIBLE);
expandedImageView.setPivotX(0f);
expandedImageView.setPivotY(0f);
AnimatorSet=新的AnimatorSet();
游戏(
卸载(expandedImageView,View.X,
起始边界(左,最终边界(左))
.with(ObjectAnimator.offload)(expandedImageView,View.Y,
startBounds.top,finalBounds.top)
.with(ObjectAnimator.offload)(expandedImageView,View.scalex,
startScale,1楼)
.with(ObjectAnimator.offload)(expandedImageView,View.SCALE_Y,
startScale,1f);
set.setDuration(mShortAnimationDuration);
set.setInterpolator(新减速器Interpolator());
set.addListener(新的AnimatorListenerAdapter(){
@凌驾
AnimationEnd上的公共无效(Animator动画){
mCurrentAnimator=null;
}
@凌驾
AnimationCancel上的公共无效(Animator动画){
mCurrentAnimator=null;
}
});
set.start();
mCurrentAnimator=设置;
最终浮球startScaleFinal=startScale;
expandedImageView.setOnClickListener(新视图.OnClickListener()的名称){
@凌驾
公共void onClick(视图){
if(mCurrentAnimator!=null){
mCurrentAnimator.cancel();
}
AnimatorSet=新的AnimatorSet();
游戏(
卸载(expandedImageView,View.X,
开始边界(左)
.with(ObjectAnimator.offload)(expandedImageView,View.Y,
// Set on item click listener to the ListView
gridFu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// Display the zoomed in image in full screen
zoomImageFromThumb(v, Fun[pos]);
}
});
// Set on item click listener to the ListView
gridFu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private Random random = new Random();
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// Display the zoomed in image in full screen
zoomImageFromThumb(v, Fun[random.nextInt(Fun.length)]);
}
});