Android:ViewGroup背景透明度

时间:2016-05-04 15:05:51

标签: android animation background

我遇到透明CircleView的问题(它扩展了ImageView并且是正方形)。我在CircleView上制作了一个圆形图像。我遇到的问题如下:

你看到的灰色背景是一个RelativeLayout,我希望隐藏每个角落,这要归功于我的CircleView的onDraw,因为有了这个功能,我将能够翻译图像,而不会在圆圈外显示它。

<RelativeLayout
    android:id="@+id/wheel_layout"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="0.45"
    android:background="#4F4F4F">

    <CircleView
        android:id="@+id/wheel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#FFF"
        android:text="\?"
        android:textStyle="bold"
        android:textSize="66dp"
        android:gravity="center_vertical|center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

如果需要我的布局:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.twit_list);
    activity = this;

    Key = getStringFromManifest("CONSUMER_KEY");
    Secret = getStringFromManifest("CONSUMER_SECRET");

    txtSearch = (EditText) findViewById(R.id.txtSearch);
    searchbtn = (Button) findViewById(R.id.searchbtn);
    save = (Button) findViewById(R.id.save);
    savedSearches = (Button)findViewById(R.id.savedSearches);

    searchbtn.setOnClickListener(new View.OnClickListener(){
        @Override
    public void onClick(View view){

            downloadSearches();
            new GoogleSearch();


        }
    });
    save.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveSearch();

        }
    });

    savedSearches.setOnClickListener(new Button.OnClickListener(){
        @Override
    public void onClick(View v){
            openSavedSearches();
        }
    });

}

所以我想要的是在RelativeLayout的背景上绘制一个透明的背景 ,你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您应该在onDraw()方法中执行类似的操作:

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = bitmap.getHeight() / 2;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

阅读本文以获取更多信息:PorterDuff