2015-08-12 2 views
0

Я написал код, который отражает представление ImageView в активности всплеска. Чтобы реализовать это, я сделал три шага.Android: создать зеркальное изображение вида

  1. Создайте ImageView в файле layout_splash.xml, а также в SplashActivity.java.
  2. Вызвать метод setImageBitmap с отраженной переменной ImageView
  3. Объявите метод, который отражает представление изображения.

И вот код.

public class SplashActivity extends Activity { 

    private ImageView ivLogo; 
    private SharedPreferences appPreferences; 
    private Typeface typeface; 
    private TextView tvAppName; 
    private boolean isAppInstalled = false; 

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

     String appName = getResources().getString(R.string.app_name); 
     appPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
     isAppInstalled = appPreferences.getBoolean("isAppInstalled", false); 
     if(isAppInstalled == false) { 
      Intent shortcutIntent = new Intent(getApplicationContext(), SplashActivity.class); 
      shortcutIntent.setAction(Intent.ACTION_MAIN); 
      Intent intent = new Intent(); 
      intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
      intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName); 
      intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher)); 
      intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
      getApplicationContext().sendBroadcast(intent); 

      SharedPreferences.Editor editor = appPreferences.edit(); 
      editor.putBoolean("isAppInstalled", true); 
      editor.commit(); 
     } 

     // Set the app name's font 
     typeface = Typeface.createFromAsset(getAssets(), "fonts/Catull.ttf"); 
     tvAppName = (TextView) findViewById(R.id.tvAppName); 
     tvAppName.setTypeface(typeface); 

     Bitmap originalImage = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_gruppo); 
     ivLogo = new ImageView(this); 
     ivLogo.setImageBitmap(getReflection(originalImage)); 

     Handler hd = new Handler(); 
     hd.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Intent intent = new Intent(getApplicationContext(), InitialActivity.class); 
       startActivity(intent); 

       // Implement the animation on activity change 
       overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out); 
       finish(); 
      } 
     }, 1200); 
    } 

    public Bitmap getReflection(Bitmap image) { 
     // The gap we want between the reflection and the original image 
     final int reflectionGap = 4; 

     // Get the bitmap from the mipmap folder 
     Bitmap originalImage = image; 

     int width = originalImage.getWidth(); 
     int height = originalImage.getHeight(); 

     // This will not scale but will flip on the Y axis 
     Matrix matrix = new Matrix(); 
     matrix.preScale(1, -1); 

     // Create a Bitmap with the flip matrix applied to it. 
     // We only want the bottom half of the image 
     Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 
       height/2, width, height/2, matrix, false); 

     // Create a new bitmap with same width but taller to fit reflection 
     Bitmap bitmapWithReflection = Bitmap.createBitmap(width, 
       (height + height/2), Bitmap.Config.ARGB_8888); 

     // Create a new Canvas with the bitmap that's big enough for 
     // the image plus gap plus reflection 
     Canvas canvas = new Canvas(bitmapWithReflection); 
     // Draw in the original image 
     canvas.drawBitmap(originalImage, 0, 0, null); 
     //Draw the reflection Image 
     canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 

     // Create a shader that is a linear gradient that covers the reflection 
     Paint paint = new Paint(); 
     LinearGradient shader = new LinearGradient(0, 
       originalImage.getHeight(), 0, bitmapWithReflection.getHeight() 
       + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); 
     // Set the paint to use this shader (linear gradient) 
     paint.setShader(shader); 
     // Set the Transfer mode to be porter duff and destination in 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
     // Draw a rectangle using the paint with our linear gradient 
     canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 
       + reflectionGap, paint); 
     return bitmapWithReflection; 
    } 
} 

Но при запуске приложения я не вижу отраженного изображения, а просто одно. Что-то не так с кодом?

+1

Поскольку 'ivLogo' не добавлял в поле зрения контента в любом месте, поэтому он не будет отображаться на экран. – Minhtdh

+0

Oh jesus. Проблемы всегда происходят в очень маленьких вещах. Я только что нашел его и исправил. Спасибо! – MarshallLee

ответ

0

Проблемы всегда происходят из самых маленьких вещей. Здесь проблема заключается в том, что переменная ivLogo не была добавлена ​​в представление содержимого. Таким образом, ivLogo должен быть добавлен в OnCreate метод, как это,

ivLogo = (ImageView) findViewById(R.id.ivLogo); 

вместо

ivLogo = new ImageView(); 
Смежные вопросы