Android:创建视图的镜像



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

  1. 在layout_splash.xml文件和SplashActivity.java中创建一个ImageView .
  2. 使用反射的ImageView变量调用setImageBitmap方法
  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;
    }
}

但是当运行应用程序时,我看不到反射的图像,而只是普通的图像。代码有问题吗?

问题总是从小事开始。这里的问题是没有将ivLogo变量添加到内容视图中。所以ivLogo必须像这样添加到onCreate方法中,

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

代替

ivLogo = new ImageView();

最新更新