即使在实现回调之后,毕加索的共享元素转换也无法正常工作

时间:2015-12-26 13:26:59

标签: java android picasso shared-element-transition

图像完美地将第一次移动到其位置,即细节活动图像视图,并且还完美地返回到主要活动,但是当我点击相同的图像下次过渡动画将图像移动到细节活动中的不正确(太高)偏移,并且一旦动画完成,图像将显示为"变形"进入正确的位置。

这是我的DetailActivity.java文件:

package com.akshitjain.popularmovies;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;


import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class DetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        postponeEnterTransition();
    }

    ImageView backdropImageView = (ImageView)       findViewById(R.id.detail_image_view);
    final ImageView posterImageView = (ImageView) findViewById(R.id.detail_poster);
    TextView overviewTextView = (TextView) findViewById(R.id.overview_text_view);
    TextView releaseDateTextView = (TextView) findViewById(R.id.release_date);
    TextView userRatingTextView = (TextView) findViewById(R.id.user_rating);
    TextView genreTextView = (TextView) findViewById(R.id.genre);
    genreTextView.setText("");
    Genre genreObject = new Genre();
    String genreName;

    Intent intent = getIntent();

    if (intent != null && intent.hasExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA)) {
        Movies movies = intent.getParcelableExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA);
        int[] genre;
        genre = movies.genre;
        for (int i = 0; i < genre.length; ++i) {
            genreName = genreObject.getGenreName(genre[i]);
            genreTextView.append(genreName);
            if (i < genre.length - 1) {
                genreTextView.append(", ");
            }
        }

        setTitle(movies.originalTitle);
        Picasso.with(getApplicationContext())
                .load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_LARGE).trim() + movies.backdropPath)
                .into(backdropImageView);
        Picasso.with(getApplicationContext())
                .load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_SMALL).trim() + movies.posterPath)
                .into(posterImageView, new Callback() {
                            @Override
                            public void onSuccess() {
                                posterImageView.getViewTreeObserver().addOnPreDrawListener(
                                        new ViewTreeObserver.OnPreDrawListener() {
                                            @Override
                                            public boolean onPreDraw() {
                                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                                    posterImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                                                    startPostponedEnterTransition();
                                                }
                                                return true;
                                            }
                                        }

                                );
                            }

                            @Override
                            public void onError() {
                            }
                        }

                );
        overviewTextView.setText(movies.overview);
        releaseDateTextView.setText(movies.releaseDate);
        userRatingTextView.setText(movies.userRating);
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_detail, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch (id) {
        case android.R.id.home:
            supportFinishAfterTransition();
            return true;
        case R.id.action_settings:
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:1)

试试这个。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>

void error(char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n, k;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    portno = 8888;
    /* Create Socket */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    /* Catch Server IP */
    server = gethostbyname("localhost");
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    while(1)
    {

        int read_size =recv(sockfd , buffer , 256 , 0);
        if (read_size>0) 
        {
            // show hint in client..
            printf("%s\n", buffer);
            if (atoi(buffer)==1) 
                printf("FIRST HINT\n");
            else if (atoi(buffer)==2)
                printf("SECOND HINT\n");

            bzero(buffer,256);
            // Enter something..
            printf("Please enter the message: ");
            bzero(buffer,256);
            fgets(buffer,255,stdin);
            n = write(sockfd,buffer,strlen(buffer));
            // bzero(buffer,256);
            if (n < 0)  error("ERROR writing to socket");
        }
        else ;
    }
    return 0;
}