Android:创建视图的镜像

时间:2015-08-12 10:29:40

标签: java android

我编写了一个代码,反映了Splash活动中ImageView的视图。为了实现这一点,我采取了三个步骤。

  1. 在layout_splash.xml文件和SplashActivity.java中创建一个ImageView。
  2. 使用反映的ImageView变量
  3. 调用setImageBitmap方法
  4. 声明反映图像视图的方法。
  5. 这是代码。

    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 个答案:

答案 0 :(得分:0)

问题总是从非常小的事情发生。在这里问题是ivLogo变量没有添加到内容视图中。因此必须在onCreate方法中添加ivLogo,如下所示,

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

而不是

ivLogo = new ImageView();
相关问题