|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 wintmain |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.wintmain.wBasis.transition; |
| 18 | + |
| 19 | +import android.content.Intent; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.view.View; |
| 22 | +import android.view.ViewGroup; |
| 23 | +import android.widget.*; |
| 24 | +import androidx.annotation.Nullable; |
| 25 | +import androidx.appcompat.app.AppCompatActivity; |
| 26 | +import androidx.core.app.ActivityCompat; |
| 27 | +import androidx.core.app.ActivityOptionsCompat; |
| 28 | +import androidx.core.util.Pair; |
| 29 | +import com.google.android.catalog.framework.annotations.Sample; |
| 30 | +import com.squareup.picasso.Picasso; |
| 31 | +import com.wintmain.wBasis.R; |
| 32 | +import com.wintmain.wBasis.transition.util.SceneTransitionBasicItem; |
| 33 | + |
| 34 | +/** |
| 35 | + * Displays a grid of items which an image and title. |
| 36 | + * When the user clicks on an item, {@link SceneTransitionBasicDetailActivity} is launched, |
| 37 | + * using the Activity Scene Transitions framework to animatedly do so. |
| 38 | + */ |
| 39 | +@Sample(name = "ActivitySceneTransitionBaic", |
| 40 | + description = "Activity Scene的变化", |
| 41 | + tags = {"android-samples", "animation-samples"} |
| 42 | +) |
| 43 | +public class SceneTransitionBasicMainActivity extends AppCompatActivity { |
| 44 | + |
| 45 | + private final AdapterView.OnItemClickListener mOnItemClickListener |
| 46 | + = new AdapterView.OnItemClickListener() { |
| 47 | + |
| 48 | + /** |
| 49 | + * Called when an item in the {@link android.widget.GridView} is clicked. Here will launch |
| 50 | + * the {@link SceneTransitionBasicDetailActivity}, using the Scene Transition animation |
| 51 | + * functionality. |
| 52 | + */ |
| 53 | + @Override |
| 54 | + public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
| 55 | + SceneTransitionBasicItem sceneTransitionBasicItem = |
| 56 | + (SceneTransitionBasicItem) adapterView.getItemAtPosition(position); |
| 57 | + |
| 58 | + // Construct an Intent as normal |
| 59 | + Intent intent = new Intent(SceneTransitionBasicMainActivity.this, |
| 60 | + SceneTransitionBasicDetailActivity.class); |
| 61 | + intent.putExtra(SceneTransitionBasicDetailActivity.EXTRA_PARAM_ID, |
| 62 | + sceneTransitionBasicItem.getId()); |
| 63 | + |
| 64 | + // BEGIN_INCLUDE(start_activity) |
| 65 | + /* |
| 66 | + * Now create an {@link android.app.ActivityOptions} instance using the |
| 67 | + * {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory |
| 68 | + * method. |
| 69 | + */ |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + ActivityOptionsCompat activityOptions = |
| 72 | + ActivityOptionsCompat.makeSceneTransitionAnimation( |
| 73 | + SceneTransitionBasicMainActivity.this, |
| 74 | + |
| 75 | + // Now we provide a list of Pair items which contain the view we can |
| 76 | + // transitioning from, and the name of the view it is transitioning to, |
| 77 | + // in the launched activity |
| 78 | + new Pair<>(view.findViewById(R.id.imageview_item), |
| 79 | + SceneTransitionBasicDetailActivity.VIEW_NAME_HEADER_IMAGE), |
| 80 | + new Pair<>(view.findViewById(R.id.textview_name), |
| 81 | + SceneTransitionBasicDetailActivity.VIEW_NAME_HEADER_TITLE)); |
| 82 | + |
| 83 | + // Now we can start the Activity, providing the activity options as a bundle |
| 84 | + ActivityCompat.startActivity(SceneTransitionBasicMainActivity.this, intent, |
| 85 | + activityOptions.toBundle()); |
| 86 | + // END_INCLUDE(start_activity) |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + /** |
| 91 | + * {@link android.widget.BaseAdapter} which displays items. |
| 92 | + */ |
| 93 | + private class GridAdapter extends BaseAdapter { |
| 94 | + |
| 95 | + @Override |
| 96 | + public int getCount() { |
| 97 | + return SceneTransitionBasicItem.SceneTransitionBasicITEMS.length; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public SceneTransitionBasicItem getItem(int position) { |
| 102 | + return SceneTransitionBasicItem.SceneTransitionBasicITEMS[position]; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public long getItemId(int position) { |
| 107 | + return getItem(position).getId(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public View getView(int position, View view, ViewGroup viewGroup) { |
| 112 | + if (view == null) { |
| 113 | + view = getLayoutInflater().inflate(R.layout.activity_scene_transition_basic_grid_item, viewGroup, false); |
| 114 | + } |
| 115 | + |
| 116 | + final SceneTransitionBasicItem item = getItem(position); |
| 117 | + |
| 118 | + // Load the thumbnail image |
| 119 | + ImageView image = (ImageView) view.findViewById(R.id.imageview_item); |
| 120 | + Picasso.with(image.getContext()).load(item.getThumbnailUrl()).into(image); |
| 121 | + |
| 122 | + // Set the TextView's contents |
| 123 | + TextView name = (TextView) view.findViewById(R.id.textview_name); |
| 124 | + name.setText(item.getName()); |
| 125 | + |
| 126 | + return view; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 132 | + super.onCreate(savedInstanceState); |
| 133 | + setContentView(R.layout.activity_scene_transition_basic_grid); |
| 134 | + |
| 135 | + GridView gridView = findViewById(R.id.grid); |
| 136 | + gridView.setOnItemClickListener(mOnItemClickListener); |
| 137 | + |
| 138 | + // 没有adapter,页面是空白的 |
| 139 | + GridAdapter mAdapter = new GridAdapter(); |
| 140 | + gridView.setAdapter(mAdapter); |
| 141 | + } |
| 142 | +} |
0 commit comments