我有水平布局,通过在线性布局上添加图像来动态添加图像视图。并且所有水平视图都垂直添加scorllview
||||||| when touch then change colored to grayscred this view(view added images here)
|||||||
|||||||
|||||||
像这样,
现在,如果我在触摸水平scoll视图,我想改变defualt图像被着色为灰度(黑色和白色)。中 ontouch on horizontal scorllview put但无法重新调整。而不是改变效果。我做了什么。
我的主要意图是: 当Horizontall滚动图像时,只有窗口视图(触摸视图)灰度级保持前一阶段(彩色)。
这是我的代码适用于图像,但我想在水平视图上。
public class FragmentOne extends Fragment {
ImageView ivIcon;
TextView tvItemName;
public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";
public HorizontalScrollView horizontalScrollView;
public ScrollView scollView;
RelativeLayout relativeSubMenu;
RelativeLayout relativeLayoutMain;
public Context mcontext;
private LinearLayout linearLayout;
public FragmentOne() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_one, container,
false);
mcontext = Common.getContext();
relativeLayoutMain = (RelativeLayout) view.findViewById(R.id.rl);
// scollView=(ScrollView) view.findViewById(R.id.verticalScroll);
// bottom linear layout for yello ads
LinearLayout linearBottom = new LinearLayout(mcontext);
linearBottom.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linearBottom.setBackgroundColor(Color.YELLOW);
linearBottom.setGravity(Gravity.BOTTOM);
linearBottom.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 5; i++) {
TextView tv= new TextView(mcontext);
tv.setTag(i);
tv.setText("Yelllo Ad "+i);
linearBottom.addView(tv);
}
//end
ScrollView scrollView = new ScrollView(mcontext);
scrollView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
LinearLayout linearLayourScrollView = new LinearLayout(mcontext);
linearLayourScrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayourScrollView.setOrientation(1);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
for (int s = 0; s < 7; s++) {
final HorizontalScrollView horizontalScrollView = new HorizontalScrollView(mcontext);
relativeSubMenu = new RelativeLayout(mcontext);
horizontalScrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout = new LinearLayout(mcontext);
linearLayout.setTag(s);
//linearLayout.setOntouchListener(this)
linearLayout.setFocusableInTouchMode(true);
//linearLayout.setBackgroundResource(R.drawable.backgroundcolor);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(0);
horizontalScrollView.setTag(s);
horizontalScrollView.setTag(s);
for (int h = 0; h < 10; h++) {
final ImageView imageView = new ImageView(mcontext);
imageView.setTag(h);
imageView.setImageResource(R.drawable.images);
/*Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = r.getDrawable(R.drawable.t);
layers[1] = r.getDrawable(R.drawable.tt);
LayerDrawable layerDrawable = new LayerDrawable(layers);
testimage.setImageDrawable(layerDrawable);*/
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mcontext, "Category="+horizontalScrollView.getTag()+" ad id="+imageView.getTag(), Toast.LENGTH_SHORT).show();
showShortDetails( horizontalScrollView.getTag(),imageView.getTag());
}
});
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
ImageView view = (ImageView) v;
//overlay is black with transparency of 0x77 (119)
// view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.DST_ATOP);
//ImageView Sun = (ImageView)findViewById(R.id.sun);
view.getDrawable().setColorFilter(ColorFilterGenerator.adjustHue(333));
view.invalidate();
//linearLayout.setBackgroundColor(Color.RED);
Log.i("Relative", "On Touch");
//hsv.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
//clear the overlay
view.getDrawable().clearColorFilter();
view.invalidate();
//relativeSubMenu.setBackgroundColor(Color.parseColor("#00FFFF00"));
//linearLayout.setBackgroundColor(Color.WHITE);
Log.i("Relative", "No Touch");
break;
}
}
return false;
}
});
linearLayout.addView(imageView);
}
horizontalScrollView.addView(linearLayout);
relativeSubMenu.addView(horizontalScrollView);
linearLayourScrollView.addView(relativeSubMenu);
}
scrollView.addView(linearLayourScrollView);
relativeLayoutMain.addView(scrollView);
relativeLayoutMain.addView(linearBottom, params);
return view;
}
答案 0 :(得分:2)
您可以在xml文件中添加布局背景,并为此背景设置自定义布局
android:background="@drawable/custombackground"
custombackground.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:state_enabled="true"
android:color="your color code" />
<item
android:state_window_focused="false"
android:state_enabled="false"
android:color="your color code" />
<item
android:state_pressed="true"
android:color="your color code" />
<item
android:state_enabled="true"
android:state_focused="true"
android:color = "#00000000"/>
<item
android:state_enabled="true"
android:color="your color code" />
<item
android:state_focused="true"
android:color="your color code" />
<item
android:color="your color code" />
</selector>
希望它会对你有所帮助:)。
答案 1 :(得分:0)
基本上,您需要在触摸时更改imageview的颜色,因为您必须在case MotionEvent.ACTION_DOWN:
中实现颜色更改代码
例如,变化的颜色是浅灰色
所以代码将是
v.setBackgroundColor(Color.LTGRAY);
您可以根据您的要求更改颜色
之后,将以下代码放入case MotionEvent.ACTION_UP:
将颜色更改为默认值,即transperent
v.setBackgroundColor(Color.TRANSPARENT);