Android将字符串转换为图片资源id,并使用Glide加载

    @OnClick({
            R.id.topBg,
            //顶部的三个按钮
            R.id.btn_tools, R.id.btn_album, R.id.btn_video})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBg:
                break;
            case R.id.btn_video:
                changeBgImg(1);
                break;
            case R.id.btn_tools:
                changeBgImg(2);
                break;
            case R.id.btn_album:
                changeBgImg(3);
                break;

        }
    }

    private void changeBgImg(int id) {
        String imageName = "bg_make_story_"+id;
//        Toast.makeText(application, "getResource(imageName):"+getResource(imageName), Toast.LENGTH_SHORT).show();
        Glide.with(this)
                .load(getResource(imageName))
                .crossFade()
                .animate(animationObject)
                .skipMemoryCache(true)
                .into(topBg);
    }
    
    ViewPropertyAnimation.Animator animationObject = new ViewPropertyAnimation.Animator() {
        @Override
        public void animate(View view) {
            // if it's a custom view class, cast it here
            // then find subviews and do the animations
            // here, we just use the entire view for the fade animation
            view.setAlpha( 0f );

            ObjectAnimator fadeAnim = ObjectAnimator.ofFloat( view, "alpha", 0f, 1f );
            fadeAnim.setDuration( 2500 );
            fadeAnim.start();
        }
    };
    
    public int getResource(String imageName){
        Context ctx=getBaseContext();
        int resId = getResources().getIdentifier(imageName, "mipmap", ctx.getPackageName());
        //如果没有在"mipmap"下找到imageName,将会返回0
        return resId;
    }

你可能感兴趣的:(Android基础)