热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Activity跳转动画无缝衔接

1234.gif本篇只贴代码,该有的注释都已写好,伸手党看过来~~~activity_main.xml:MainActivity:public class MainActivity extends



Activity跳转动画 无缝衔接


1234.gif


本篇只贴代码,该有的注释都已写好,伸手党看过来~~~

activity_main.xml:



MainActivity:


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private static final String TAG = "MainActivity";

private GridView mGridView;
private GridAdapter mAdapter;

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

mGridView = (GridView) findViewById(R.id.grid);
mGridView.setOnItemClickListener(this);
mAdapter = new GridAdapter();
mGridView.setAdapter(mAdapter);
}

@Override
public void onItemClick(AdapterView> adapterView, View view, int position, long id) {
Item item = (Item) adapterView.getItemAtPosition(position);

// Construct an Intent as normal
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_PARAM_ID, item.getId());

// BEGIN_INCLUDE(start_activity)
/**
* Now create an {@link android.app.ActivityOptions} instance using the
* {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory
* method.
*/
ActivityOptionsCompat activityOptiOns= ActivityOptionsCompat.makeSceneTransitionAnimation(
this,

// Now we provide a list of Pair items which contain the view we can transitioning
// from, and the name of the view it is transitioning to, in the launched activity
new Pair(view.findViewById(R.id.imageview_item),
DetailActivity.VIEW_NAME_HEADER_IMAGE),
new Pair(view.findViewById(R.id.textview_name),
DetailActivity.VIEW_NAME_HEADER_TITLE));

// Now we can start the Activity, providing the activity options as a bundle
ActivityCompat.startActivity(this, intent, activityOptions.toBundle());
// END_INCLUDE(start_activity)
}
}

GridAdapter :


private class GridAdapter extends BaseAdapter {

@Override
public int getCount() {
return Item.ITEMS.length;
}

@Override
public Item getItem(int position) {
return Item.ITEMS[position];
}

@Override
public long getItemId(int position) {
return getItem(position).getId();
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
view = getLayoutInflater().inflate(R.layout.grid_item, viewGroup, false);
}

final Item item = getItem(position);

// Load the thumbnail image
ImageView image = (ImageView) view.findViewById(R.id.imageview_item);
Picasso.with(image.getContext()).load(item.getThumbnailUrl()).into(image);


// Set the TextView's contents
TextView name = (TextView) view.findViewById(R.id.textview_name);
name.setText(item.getName());

return view;
}
}

grid_item.xml



DetailActivity


public class DetailActivity extends Activity {

// Extra name for the ID parameter
public static final String EXTRA_PARAM_ID = "detail:_id";

// View name of the header image. Used for activity scene transitions
public static final String VIEW_NAME_HEADER_IMAGE = "detail:header:image";

// View name of the header title. Used for activity scene transitions
public static final String VIEW_NAME_HEADER_TITLE = "detail:header:title";

private ImageView mHeaderImageView;
private TextView mHeaderTitle;

private Item mItem;

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

// Retrieve the correct Item instance, using the ID provided in the Intent
mItem = Item.getItem(getIntent().getIntExtra(EXTRA_PARAM_ID, 0));

mHeaderImageView = (ImageView) findViewById(R.id.imageview_header);
mHeaderTitle = (TextView) findViewById(R.id.textview_title);

// BEGIN_INCLUDE(detail_set_view_name)
/**
* Set the name of the view's which will be transition to, using the static values above.
* This could be done in the layout XML, but exposing it via static variables allows easy
* querying from other Activities
*/
ViewCompat.setTransitionName(mHeaderImageView, VIEW_NAME_HEADER_IMAGE);
ViewCompat.setTransitionName(mHeaderTitle, VIEW_NAME_HEADER_TITLE);
// END_INCLUDE(detail_set_view_name)

loadItem();
}

private void loadItem() {
// Set the title TextView to the item's name and author
mHeaderTitle.setText(getString(R.string.image_header, mItem.getName(), mItem.getAuthor()));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && addTransitionListener()) {
// If we're running on Lollipop and we have added a listener to the shared element
// transition, load the thumbnail. The listener will load the full-size image when
// the transition is complete.
loadThumbnail();
} else {
// If all other cases we should just load the full-size image now
loadFullSizeImage();
}
}

/**
* Load the item's thumbnail image into our {@link ImageView}.
*/
private void loadThumbnail() {
Picasso.with(mHeaderImageView.getContext())
.load(mItem.getThumbnailUrl())
.noFade()
.into(mHeaderImageView);
}

/**
* Load the item's full-size image into our {@link ImageView}.
*/
private void loadFullSizeImage() {
Picasso.with(mHeaderImageView.getContext())
.load(mItem.getPhotoUrl())
.noFade()
.noPlaceholder()
.into(mHeaderImageView);
}

/**
* Try and add a {@link Transition.TransitionListener} to the entering shared element
* {@link Transition}. We do this so that we can load the full-size image after the transition
* has completed.
*
* @return true if we were successful in adding a listener to the enter transition
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean addTransitionListener() {
final Transition transition = getWindow().getSharedElementEnterTransition();

if (transition != null) {
// There is an entering shared element transition so add a listener to it
transition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionEnd(Transition transition) {
// As the transition has ended, we can now load the full-size image
loadFullSizeImage();

// Make sure we remove ourselves as a listener
transition.removeListener(this);
}

@Override
public void onTransitionStart(Transition transition) {
// No-op
}

@Override
public void onTransitionCancel(Transition transition) {
// Make sure we remove ourselves as a listener
transition.removeListener(this);
}

@Override
public void onTransitionPause(Transition transition) {
// No-op
}

@Override
public void onTransitionResume(Transition transition) {
// No-op
}
});
return true;
}

// If we reach here then we have not added a listener
return false;
}

}

details.xml



Item


public class Item {

private static final String LARGE_BASE_URL = "http://storage.googleapis.com/androiddevelopers/sample_data/activity_transition/large/";
private static final String THUMB_BASE_URL = "http://storage.googleapis.com/androiddevelopers/sample_data/activity_transition/thumbs/";

public static Item[] ITEMS = new Item[] {
new Item("Flying in the Light", "Romain Guy", "flying_in_the_light.jpg"),
new Item("Caterpillar", "Romain Guy", "caterpillar.jpg"),
new Item("Look Me in the Eye", "Romain Guy", "look_me_in_the_eye.jpg"),
new Item("Flamingo", "Romain Guy", "flamingo.jpg"),
new Item("Rainbow", "Romain Guy", "rainbow.jpg"),
new Item("Over there", "Romain Guy", "over_there.jpg"),
new Item("Jelly Fish 2", "Romain Guy", "jelly_fish_2.jpg"),
new Item("Lone Pine Sunset", "Romain Guy", "lone_pine_sunset.jpg"),
};

public static Item getItem(int id) {
for (Item item : ITEMS) {
if (item.getId() == id) {
return item;
}
}
return null;
}

private final String mName;
private final String mAuthor;
private final String mFileName;

Item(String name, String author, String fileName) {
mName = name;
mAuthor = author;
mFileName = fileName;
}

public int getId() {
return mName.hashCode() + mFileName.hashCode();
}

public String getAuthor() {
return mAuthor;
}

public String getName() {
return mName;
}

public String getPhotoUrl() {
return LARGE_BASE_URL + mFileName;
}

public String getThumbnailUrl() {
return THUMB_BASE_URL + mFileName;
}

}

SquareFrameLayout


public class SquareFrameLayout extends FrameLayout {

public SquareFrameLayout(Context context) {
super(context);
}

public SquareFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

if (widthSize == 0 && heightSize == 0) {
// If there are no constraints on size, let FrameLayout measure
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

// Now use the smallest of the measured dimensions for both dimensions
final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(minSize, minSize);
return;
}

final int size;
if (widthSize == 0 || heightSize == 0) {
// If one of the dimensions has no restriction on size, set both dimensions to be the
// on that does
size = Math.max(widthSize, heightSize);
} else {
// Both dimensions have restrictions on size, set both dimensions to be the
// smallest of the two
size = Math.min(widthSize, heightSize);
}

final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(newMeasureSpec, newMeasureSpec);
}
}



推荐阅读
  • c++:1
    C第一部分介绍基础:c++:-0,本节介绍C中函数使用。##函数###函数调用调用函数需要先声明函数原型嵌套调用:###参数传递在函数被调用时才分配形参的存储单元实参可以是常量、变 ... [详细]
  • 在一个N×N的网格中,每个单元格(x, y)(其中0 ≤ x < N 和 0 ≤ y < N)都可能有一个灯泡。初始状态下,某些灯泡是亮着的。当某个灯泡亮起时,它会照亮其所在的行、列和两条对角线上的所有单元格。对于一系列查询,需要确定每个查询的单元格是否被照亮,并在每次查询后关闭该单元格及其相邻单元格上的灯泡。 ... [详细]
  • 深入理解运算符及其应用
    本文详细介绍了运算符的基本概念,特别是算术运算符在编程中的具体应用及注意事项。例如,在进行整数除法时,结果默认为整数,这是由于数据类型的限制所致。文章还通过具体的代码示例解释了如何利用运算符解决实际问题。 ... [详细]
  • 一、搭建项目创建Maven项目导入rabbitmq包com.rabbitmqamqp-clien ... [详细]
  • MD5(Message-Digest Algorithm 5),即消息摘要算法第五版,是一种广泛应用于计算机安全领域的散列函数,主要用于确保数据传输的完整性和验证数据的一致性。本文将介绍如何在Java编程环境中实现MD5加密。 ... [详细]
  • Spring Cloud实践:构建Eureka单节点注册中心
    本文详细介绍如何在Spring Cloud环境下搭建Eureka单节点注册中心,包括项目初始化、依赖添加、配置设置及启动测试等步骤。 ... [详细]
  • 本文介绍了在Makefile及Android.mk文件中添加打印输出信息的方法,并详细解析了Android编译过程中的关键步骤,包括环境变量的设置与编译脚本的执行。 ... [详细]
  • 如何构建基于Dubbo协议的示例项目
    本文详细介绍了构建基于Dubbo协议的示例项目的步骤,包括环境搭建、服务接口定义、服务实现、配置文件设置及客户端调用等环节,旨在为初学者提供一个清晰的学习路径。 ... [详细]
  • 本文探讨了如何使用Go语言从传统的INI配置文件中提取所需的信息。INI文件因其简单易用而在多种环境中广泛采用,如操作系统设置、游戏引擎配置以及版本控制系统等。 ... [详细]
  • 部署新的ASP.NET Web应用程序构建(主要涉及DLL文件更改)后,服务器上的CPU使用率每几秒就会飙升至100%,问题似乎源自lsass.exe进程。这一现象与应用程序部署之间是否存在直接关联? ... [详细]
  • 本文详细解释了 Java 编程语言中 @SuppressWarnings 注解的使用方法及其意义,特别是在处理未经检查的类型转换警告时的应用。 ... [详细]
  • 本文详细介绍了如何在VUE开发环境中正确安装和配置Nightwatch及Karma相关插件,并解决运行测试时遇到的Java版本不兼容问题。 ... [详细]
  • 本文详细介绍了在EXTJS 3.1中如何实现列锁定以及确保合计行能够随滚动条同步移动的技术方案。 ... [详细]
  • 本文探讨了Java和C#中可变参数的使用规则及示例代码,重点介绍了两种语言中实现可变参数的不同方式及其限制条件。 ... [详细]
  • DataList内容详解
    DataList是另一种显示数据控件,它与GridView不同的是,它全部使用模板进行设计,并且DataList的模板是对整行设置 ... [详细]
author-avatar
走下去就下去
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有